how to insert null when value is empty

Hi All

I have this model with the following two fields (SQL):


....

`created_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',

`update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,

....



The problem is that ‘created_time’ only gets its time when it is set to null, as follows


INSERT INTO user ( ......, created_time) VALUES( ......, NULL) ;



If you ommit the created_time, the field will be set to its default value: ‘0000-00-00 00:00:00’!

So the question is, how can I let Yii implement this behavior by default for all models (I have a couple of tables/models that have these fields)

cheers

Luca

I thought when you set NOT NULL when creating table, isn’t it mean that you can’t give null value to that field?

maybe you can implement assignment on beforeSave() method in the model…


function beforeSave(){

    $this->created_time = null;

    parent::beforeSave();

}

haven’t try it yet, but maybe something like that…

As I see this… the problem is in the database… why do you set


...NOT NULL DEFAULT '0000-00-00 00:00:00',

if you don’t want this value…

If you need the current timestamp why don’t you set this field to


...NOT NULL DEFAULT CURRENT_TIMESTAMP,

You can try using http://code.google.com/p/yiiext/downloads/detail?name=ensureNull_1.0.1.zip extension.

Thanks alot!! Although the module doesn’t really works in this particular situation, it helped me to add this functionality to my extension!

Please checkout: EAdvancedArBehavior

cheers