yii: created and updated, who and when

by prettyscripts on 2010-09-02 15:31:02

phpyii

most data have created_date, created_by, updated_date and updated_by columns to audit records. in yii, these columns can be automatically updated via these 2 behaviors:

  • CTimestampBehavior - a standard zii behavior that is distributed with yii
  • BlameableBehavior - a user contributed extension. download from this page and unzip to /path/to/components/behaviors

edit /path/to/models/<Model>.php and add / edit behavior function:

PHP:

public function behaviors()
{
    return array(
        'CTimestampBehavior' => array(
            'class' => 'zii.behaviors.CTimestampBehavior',
            'createAttribute' => 'created_date',
            'updateAttribute' => 'modified_date',
            'setUpdateOnCreate' => true,
        ),
        'BlameableBehavior' => array(
            'class' => 'application.components.behaviors.BlameableBehavior',
            'createdByColumn' => 'created_by'// optional
            'updatedByColumn' => 'modified_by',  // optional
        ),
    );
}

note

lines 6, 7: set to null if column does not exist.

line 8: update date column by default is not updated on create unless this is set to true

lines 12, 13: these are not required if the column names are 'created_by', 'modified_by'. it seems to be able to detect such column does not exist too.

now, these fields are automatically updated!

hacking blameable behavior

always update by

updatedByColumn will not be updated to current user if the field has a value, ie if it is not empty.

if you want this field to always gets updated with every data modification, go to the source code, remove the empty function checking from lines 31 and 34.

save as username instead id

add the following attribute somewhere after defining $updatedByColumn:

PHP:

public $userAttribute 'id';

find following codes:

PHP:

$this->owner->{$this->createdByColumn} = Yii::app()->user->id;
$this->owner->{$this->updatedByColumn} = Yii::app()->user->id;

replace 'id' with {$this->userAttribute}, so the code looks like:

PHP:

$this->owner->{$this->createdByColumn} = Yii::app()->user->{$this->userAttribute};
$this->owner->{$this->updatedByColumn} = Yii::app()->user->{$this->userAttribute};

edit /path/to/models/<Model>.php and add / edit behavior function to set userAttribute:

PHP:

public function behaviors()
{
    return array(
        'BlameableBehavior' => array(
            'class' => 'application.components.behaviors.BlameableBehavior',
            'createdByColumn' => 'created_by',
            'updatedByColumn' => 'modified_by',
            'userAttribute' => 'name'// <-- note this
        ),
    );
}

Tags: behavior, created, extension, modified, timestamp, updated, yii

5 comments

Comment by cactork @ 2010-10-29 12:52:25
****-
Very useful!
Thanks for share!
Comment by cactork @ 2010-10-29 15:36:50
Hi,
I'm using Yii v1.1 and it throws me an exception in the line 6:
'createdAttribute'=>'create_date',

It works for me with "createAttribute".
Thanks again,
Comment by prettyscripts @ 2010-10-30 01:58:33
@cactork, thanks for bringing that to my attention. codes updated.
Comment by Beginner @ 2011-07-29 03:59:00
Hello :)
How could I have the results ( created_date and modified_date) in the french format ( day / month/ year hour/minuts/seconds ) ??
Please !!

A french Beginner :D
Comment by prettyscripts @ 2011-07-29 09:53:17
@Beginner, you can call mysql function date_Format() to display date in the required format. hope this helps.

Leave a comment


Your email address will not be revealed on this site.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)