Difference between #7 and #8 of
How to log changes of ActiveRecords?

Revision #8 has been created by wei on Feb 14, 2009, 10:27:17 PM with the memo:

Update code highlighting
« previous (#7) next (#9) »

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):     [sql]
 
CREATE TABLE ActiveRecordLog (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
description VARCHAR(255) NULL,
[...]
```php
class ActiveRecordLogableBehavior extends CActiveRecordBehavior
    { private $_oldattributes = array();
 
public function afterSave($event) {
 
if (!$this->Owner->isNewRecord) {

// new attributes
[...]
// compare old and new
foreach ($newattributes as $name => $value) {
if (!empty($oldattributes)) {
$old = $oldattributes[$name];
[...]
}


 
if ($value != $old) { //$changes = $name . ' ('.$old.') => ('.$value.'), '; $log=new ActiveRecordLog; $log->description= 'User ' . Yii::app()->user->Name . ' changed ' . $name . ' for ' . get_class($this->Owner)
 
                                            . ' changed ' . $name . ' for ' 
 
                                            . get_class($this->Owner) 
 
                                           
. '[' . $this->Owner->getPrimaryKey() .'].';
$log->action= 'CHANGE';
$log->model= get_class($this->Owner);
[...]
$log->userid= Yii::app()->user->id;
$log->save();

 
 
 
} } } else { $log=new ActiveRecordLog; $log->description= 'User ' . Yii::app()->user->Name
 
                                    
. ' created ' . get_class($this->Owner)
 
                                    
. '[' . $this->Owner->getPrimaryKey() .'].';
$log->action= 'CREATE';
$log->model= get_class($this->Owner);
[...]
}


 
 
public function afterDelete($event) { $log=new ActiveRecordLog; $log->description= 'User ' . Yii::app()->user->Name . ' deleted ' . get_class($this->Owner)
 
                                . get_class($this->Owner) 
 
                               
. '[' . $this->Owner->getPrimaryKey() .'].';
$log->action= 'DELETE';
$log->model= get_class($this->Owner);
[...]
}


 
 
public function afterFind($event)
{
// Save old values
[...]
```php
public function behaviors()
    { return array(     // Classname => path to Class      'ActiveRecordLogableBehavior'=>      'application.behaviors.ActiveRecordLogableBehavior', );     } }
```

Of course this simple example could be enhanced:

+ support for mult-column primary keys
[...]
31 1
47 followers
Viewed: 72 524 times
Version: 1.1
Category: Tutorials
Tags: Logging
Written by: pfth
Last updated by: Yang He
Created on: Feb 13, 2009
Last updated: 11 years ago
Update Article

Revisions

View all history