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.
If you're installing in a code base earlier than 2.x, be sure that you use the code on the 1.3
branch.
- Click the big 'ol Downloads button next to the project description.
- Extract the archive to
app/plugins/nullable
.
$ git submodule add git://github.com/robwilkerson/CakePHP-NullableBehavior.git <path_to>/app/plugins/nullable
$ git submodule init
$ git submodule update
- Ensure that you're using the intended branch (may I suggest
master
?)
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:
-
Copy
<application root>/cake/libs/model/app_model
to your<application root>/app/
directory. -
Apply the
NullableBehavior
to the newly copiedAppModel
.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.
This code is licensed under the MIT license.
Feel free to suggest improvements in a ticket or fork this project and improve upon it yourself.