Updating MySQL TIMESTAMP columns

I usually have a timestamp column in my MySQL tables like this:

changeTime TIMESTAMP NOT NULL

which implicitely declares the column in MySQL with this attributes:

changeTime TIMESTAMP NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP

That means i can update every entry in that table and leave away the changeTime column. It will always be automatically updated to the time of last change. This works even though the column is declared as NOT NULL. MySQL takes care of everything.

With Yii's ActiveRecords i can remove the required rule for that column - that's great. I can create and save a record with NULL in the changeTime attribute and it will have the changeTime set correctly.

But when i load an existing record and save it again, the old value will remain. I have to set changeTime to either null or the current date. Wouldn't it be convenient to somehow declare this column as an autoupdated timestamp column that's set to NULL on every save()?

I've tried to workaround this issue with beforeSave():

public function beforeSave() {


    $this->changeTime=null;


    return true;


}

But now the record doesn't get saved anymore.

Sorry, i tested it with yiic shell and seems like i had to restart the shell session. It works with one minor drawback: after calling save() changeTime is still null. But that's acceptable for me.

Yeah, using beforeSave() is probably the best to handle this problem. If you want, you may call CActiveRecord::refresh() to get the up-to-date values.

Ah, ok. Thanks.