Giter Site home page Giter Site logo

biesbjerg / cakephp-nullablebehavior Goto Github PK

View Code? Open in Web Editor NEW

This project forked from redd/cakephp-nullablebehavior

0.0 1.0 0.0 173 KB

A simple behavior for CakePHP that inserts a `null` value instead of an empty string when a property value is not set and its corresponding field is nullable.

PHP 100.00%

cakephp-nullablebehavior's Introduction

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.

cakephp-nullablebehavior's People

Contributors

ingk avatar robwilkerson avatar terrabruder avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.