Null Behavior

by Jippi
Problems with default NULL fields not being very NULL'ish ?
This is just a small script to make sure some values arent saved if they are supposed to be NULL in the database.

*** Updated ***
Added check on length of string, to avoid the 0 being caught as a nullable value.

Use

Model Class:

<?php 
var $actsAs = array('Null' => array('file_id','parent_id'));
?>

Code


Model Class:

<?php 
class NullBehavior extends ModelBehavior {
    var 
$settings = array();

    
/**
     * Enter description here...
     *
     * @param AppModel $model
     * @param unknown_type $config
     */
    
function setup(&$model$config = array() )
    {
        
$this->settings$model->name ] = $config;
    }

    
/**
     * Enter description here...
     *
     * @param AppModel $model
     */
    
function beforeSave(&$model)
    {
        
$config $this->settings[$model->name];

        foreach ( 
$config AS $field )
        {
            if(
                
true === array_key_exists($field,$model->data[$model->name] ) &&
                
true === empty( $model->data[$model->name][$field] ) &&
                
=== strlen$model->data[$model->name][$field] ) )
            {
                unset(
$model->data[$model->name][$field]);
            }
        }
        return 
true;
    }
}
?>

Report

More on Behaviors

Advertising

Comments

  • manmoon posted on 05/14/09 01:47:34 AM
    I'm still relatively new to CakePHP, so maybe this can be done in a better way, but I moved Peter's code into a behavior class:


    <?php 

    /**
     * Replaces empty strings, '', with null values before committing them to the
     * database.
     *
     * SAMPLE USAGE:
     * var $actsAs = array('Null' => array('gender', 'city', 'country'));
     */
    class NullBehavior extends ModelBehavior {
        
        
    /**
         * Initializes the fields to which this behavior applies.
         */
        
    function setup(&$model$fields = array()) {
            
    $this->settings[$model->name] = $fields;
        }
        
        
    /**
         * For the appropriate fields, convert empty strings to null values before
         * saving the data.
         */
        
    function beforeSave(&$model) {
            foreach (
    $this->settings[$model->name] as $field) {
                if (isset(
    $model->data[$model->name][$field])
                       && 
    $model->data[$model->name][$field] === '') {
                    
    $model->data[$model->name][$field] = null;
                }            
            }
        }
        
    }

    ?>
  • dericknwq posted on 05/25/07 10:14:39 PM
    This code is meant to be included in your app_model.php or particular model.



        function beforeSave() {
            /**
             * Checks table metadata for fields which allows NULL and set the value
             * to NULL when they are empty
             * 
             * TODO: probably make this a behavior?
             */
            $tableInfo = $this->_tableInfo->get();
            foreach ($tableInfo as $field) {
                if ($field['null']) {
                    if (isset($this->data[$this->name][$field['name']]) && $this->data[$this->name][$field['name']] === '') {
                        $this->data[$this->name][$field['name']] = null;
                    }
                }
            }
            
            return true;
        }
    • pr1001 posted on 04/27/08 08:44:55 AM
      Derick's code needed a few tweaks to work on 1.2.0.6311 beta:

          function beforeSave() {
              /**
               * Checks table metadata for fields which allows NULL and set the value
               * to NULL when they are empty
               * 
               * TODO: probably make this a behavior?
               */
              $tableInfo = $this->schema();
              foreach ($tableInfo as $name => $field) {
                  if ($field['null']) {
                      if (isset($this->data[$this->name][$name]) && $this->data[$this->name][$name] === '') {
                          $this->data[$this->name][$name] = null;
                      }
                  }
              }
              
              return true;
          }
    • sarimarton posted on 08/08/07 09:16:36 PM
      This code is meant to be included in your app_model.php or particular model.

      Hey Derick, your code looks much more better.
  • bbuchs posted on 04/03/07 10:58:44 AM
    I often use select lists with "empty"=>true (which prepends an empty option to the list). If the field I'm trying to write to is an INT and also accepts a NULL value, Cake will try to insert an empty string (''). I made this tweak to the Null behaviour to fix that:

    //unset($model->data[$model->name][$field]);
    $model->data[$model->name][$field] = NULL;
  • francky06l posted on 03/24/07 01:15:40 PM
    I faced a similar problem with the numeric values being set to 0 instead of null (cake 1.1).
    I did something similar, but the test :
    true === empty( $model->data[$model->name][$field] )

    will be true if the value is set to 0 (string or numeric), therefore it might be impossible to save a field whose value is 0.

    Adding the following in the test expression fixes this :

    && $model->data[$model->name][$field] != "0"


login to post a comment.