1. PHP
  2. Yii Framework

Yii Framework – Automatically timestamps create and update operations

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.

  1. created_at – Automatically set to the current date and time when the record is first created.
  2. updated_at – Automatically set to the current date and time whenever the record is updated.
Comments to: Yii Framework – Automatically timestamps create and update operations

    Your email address will not be published. Required fields are marked *

    Attach images - Only PNG, JPG, JPEG and GIF are supported.