How to log changes of ActiveRecords?

Comparing #6 with #7

Revision #7 was created by jonah jonah on Feb 13, 2009, 10:48:20 PM.

Userid column now stored id instead of name & for date it uses new CDbExpression('NOW()')

Content

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 ( idActiveRecordLog 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();
}
[...]