Revision #7 has been created by jonah on Feb 13, 2009, 10:48:20 PM with the memo:
Userid column now stored id instead of name & for date it uses new CDbExpression('NOW()')
« previous (#6) next (#8) »
Changes
Title
unchanged
How to log changes of ActiveRecords?
Category
unchanged
Tutorials
Yii version
unchanged
Tags
unchanged
Content
changed
A simple and effective way to keep track what your users are doing within your application is to log their activities related to database modifications. You can log whenever a record was inserted, changed or deleted, and also when and by which user this was done. For a [CActiveRecord] Model you could use a behavior for this purpose. This way you will be able to add log functionality to ActiveRecords very easily.
First of all you have to create a table for the log-lines in the database. Here is an example (MySQL):
CREATE TABLE ActiveRecordLog (
id
ActiveRecordLog INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
description VARCHAR(255) NULL,
action VARCHAR(20) NULL,[...]
$log->idModel= $this->Owner->getPrimaryKey();
$log->field= $name;
$log->creationdate= date("Y-m-d H:i:s", timenew CDbExpression('NOW()
');
$log->userid= Yii::app()->user->
Nameid;
$log->save();[...]
$log->idModel= $this->Owner->getPrimaryKey();
$log->field= '';
$log->creationdate= date("Y-m-d H:i:s", time new CDbExpression('NOW()
');
$log->userid= Yii::app()->user->
Nameid;
$log->save();
}[...]
$log->idModel= $this->Owner->getPrimaryKey();
$log->field= '';
$log->creationdate= date("Y-m-d H:i:s", time new CDbExpression('NOW()
');
$log->userid= Yii::app()->user->
Nameid;
$log->save();
}[...]