Kohana 3.1 ORM: Empty model property value saved as 0 (zero) instead of NULL?

If so, the value of choosen option is probably set to 0 when no methodology is choosen, and send with other form data.In such cases I make a custom filter withing model's filters() method, to set the value to NULL (as PHP treats it) when it's empty(), something like this: public function filters() { return array( 'methodology_id' => array( array('Filter::null', array(':value')), ), ); } Where Filter is helper class with static methods. Like...: class Kohana_Filter { public static function null($value) { return (empty($value)? NULL : $value); } } Hope this helps :).

Thanks for your answer. I chose this one as the correct answer because it is the most elegant solution to my mind. I believe the others are correct also.

– Alejandro García Iglesias Jun 2 at 15:15.

Even if the submitted form field is blank it's a string with any empty value - not NULL. This gets cast as 0 when saved to an INT field (at least in mysql). To preserve a null value do something like this: $methodology_id = (empty($methodology_id)?

NULL : $methodology_id).

What you should do is set the key for the None value in your select box to 0. You should leave the not_empty rule for the field. Then you should have a rule for the field that makes sure that the value is either a legitimate value for product_methodology or zero.

I extend ORM and have the following two functions: public function exists_or_zero(Validation $validation, $field, $model, $pk) { // The exists() function is only called if the field has a non-zero value if ($validation$field) { $this->exists($validation, $field, $model, $pk); } } public function exists(Validation $validation, $field, $model, $pk) { if (! ORM::factory($model, $pk)->loaded()) { $validation->error($field, 'exists', array($validation$field)); } } If you were to use these functions, you're rules in the product class would look like: public function rules() return array( 'product_methodology_id' => array( array('not_empty'), array(array($this, 'exists_or_zero'), array(':validation', ':field', 'product_methodology', ':value')), ), ); }.

An example of using a closure to return a NULL value if the field value is an empty string: public function filters() { return array( // Return a NULL value if empty string. 'field_name' => array( array(function($value){ return ($value === '')? NULL : $value; }, array(':value')), ) ); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions