Skip to content

Latest commit

 

History

History
48 lines (29 loc) · 2.27 KB

README.markdown

File metadata and controls

48 lines (29 loc) · 2.27 KB

NullableBehavior

A simple behavior plugin for CakePHP 1.2/1.3/2.x that inserts a null value instead of an empty string when a property value is not set and its corresponding field is nullable.

This behavior is especially useful when foreign key fields are nullable. CakePHP attempts to insert whatever value is assigned to a property. In most cases, this value is generated by a form where empty values are represented by empty strings. If the property is a foreign key, an empty string will generate a database constraint error. A model that actsAs Nullable will not.

This project was recently moved from a simple behavior to a plugin so that it's easier to maintain and install within the context of other projects.

Installation

If you're installing in a code base earlier than 2.x, be sure that you use the code on the 1.3 branch.

As an Archive

  1. Click the big 'ol Downloads button next to the project description.
  2. Extract the archive to app/plugins/nullable.

As a Submodule

  1. $ git submodule add git://github.com/robwilkerson/CakePHP-NullableBehavior.git <path_to>/app/plugins/nullable
  2. $ git submodule init
  3. $ git submodule update
  4. Ensure that you're using the intended branch (may I suggest master?)

Usage

Nullable is one of those behaviors that really just makes an application behave the "right" way and has no negative impact. In every case, I've seen an empty string is applied as a matter of convenience rather than of correctness. To that end, the preferred usage is to apply this behavior to all models. To do so:

  1. Copy <application root>/cake/libs/model/app_model to your <application root>/app/ directory.

  2. Apply the NullableBehavior to the newly copied AppModel.

     class AppModel extends Model {
       public $actsAs = array( 'Nullable.Nullable' );
     
       # 
       # Additional shared model code, as required
       # 
     }
    

Of course, the Nullable behavior can also be applied to a specific model in the same manner outlined above.

License

This code is licensed under the MIT license.

Notes

Feel free to suggest improvements in a ticket or fork this project and improve upon it yourself.