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

Let’s Start a Project!

Contact Me