Yii - Maintaining created and updated date

A very simple way to maintain date-time stamps on records is to use the behaviors method in the data Model, linked with the CTimestampbehavior within Zii widgets.

To implement this, in each of your data models, use a behaviors function as follows:-

public function behaviors(){ return array( 'CTimestampbehavior' => array( 
       'class' => 'zii.behaviors.CTimestampbehavior', 
       'createAttribute' => null, 
       'updateAttribute' => 'last_updated', 
       'setUpdateOnCreate' => true, ) ); } 

Note that in my model, I have set the CREATE attribute to NULL as my table does not have a created_dt column.

Also, the last_updated column is updated on create by the option 'SetUpdateOnCreate' being set to TRUE.

See also how to use behaviors to manage Create and Update Dates

Did you know you can hire me?

I take on projects of all sizes. From Consulting to large Development Projects.

If you're starting a new Yii project and would like some help to get setup and running or you need some help with a particular module or you just need someone to develop the whole dang thing, then just ask ...

Leave a Comment

twitterfacebookgooglelinkedin https://me.yahoo.com