This article will show you how to automatically populates / updates created_at and updated_at column when you create or update a model instance in Yii Framework. We will set the two date/time column values on different task. so we need to use scenario as the useful tool for separating tasks on your model.
Syntax
# protected/models/YourModel.php
public function rules() {
....
array('created_at', 'default', 'value'=>new CDbExpression('NOW()'), 'on'=>'insert'),
array('updated_at', 'default', 'value'=>new CDbExpression('NOW()'), 'on'=>'update'),
}
with above code, created_at and updated_at column will managed by Active Record. ActiveRecord automatically populates / updates them when you create or update a model instance.
- created_at – Automatically set to the current date and time when the record is first created.
- updated_at – Automatically set to the current date and time whenever the record is updated.
No Comments
Leave a comment Cancel