Empty values and null values


I used the yiic shell to generate models and forms.

Now i have a problem with all fields that are not required. When i fill the form and sumbit it, it saves the textfields I left blank as an empty value in the database instead of a null value.

This doesn’t give a big problem in ordinary fields. but on a field with a not required foreign key like country or currency it is trying to put an empty value as reference to the country table in the database. there is only no empty value in de country table. so that gives an foreign key error.

The values in the database are allowed to have a null value and default value is null.

Does any one know what the solution is to this problem?

I have a similar error, when I try to assign the CDbExpression(‘NULL’) I get a “field must be a number” error.

I’ve not found a solution yet.

I’ve found the solution in http://www.yiiframework.com/forum/index.php?/topic/5448-blank-fields-in-forms-insert-replace-default-null-columns-with-empty-string/

If you need that an empty value be stored as NULL then you need to add

array('checkAccess', 'default', 'setOnEmpty' => true, 'value' => null),

and if you need to assign a NULL value in the source code, you need to assign the null directly, I mean:

$model->property = null;

I hope this answers your question.