Yii 1.1: audittrail

AuditTrail module includes behaviors, controllers, and widgets to make things easy to use!
53 followers

Update 1.2.0 This module now uses migrations for database management and does not require user ids or table ids to be numeric. Many typos fixed. Widget can now be configured for display like ZDataGrid Requires Yii 1.6 or higher

Update 1.1.1 Widget now works with blog demo, is more resource efficient. Only requires Yii 1.1

Update 1.1.0 No Longer Requires xportlet, it uses zii CPortlet instead. Only requires Yii 1.1

This is the audit trail module. My goal is to get you from scratch to a usable audit trail in less than five minutes.

This module provides basic access to any changes performed via active record through any class that has the LoggableBehavior assigned. It is based off of this cookbook article:

http://www.yiiframework.com/wiki/9/how-to-log-changes-of-activerecords

I've extended the functionality a little bit and made it easy to include audit trail records anywhere one your site, and manage the audit trail from a central location.

I also included a "Getting started" page with the module, including step by step setup instructions (although there aren't too many steps!) and bundled an installer script and detailed documentation for it's usage.

Any recommendations would be greatly appreciated!

Requirements

  • Yii 1.6 or above
  • Your tables must have a field called id that is the primary key (so audit trail can effectively log table changes)

Installation

  1. Unzip auditTrail.zip and move the auditTrail folder to your /protected/modules folder
  2. Add the following code to your /protected/config/main.php
...
    'import'=>array(
        ...
        'application.modules.auditTrail.models.AuditTrail',
        ...
    ),
    ...
    'modules'=>array(
        ...
        'auditTrail'=>array(),
        ...
    ),
    ...
  1. Visit http://yourapp/index?r=auditTrail
  2. From there, read up for more instructions and configuration options

Usage

Put this code in any Active Record Model

public function behaviors()
{
    return array(
        'LoggableBehavior'=>
            'application.modules.auditTrail.behaviors.LoggableBehavior',
    );
}

Then put this widget on any view or update page to see changes to the object

$this->widget(
    'application.modules.auditTrail.widgets.portlets.ShowAuditTrail',
    array(
        'model' => $model,
    )
);

Also there are a few more features. Download and try it to see what all it can do!

Resources

Total 20 comments

#16888 report it
Sammaye at 2014/04/09 03:30am
Re: cascade impact

I think there is a way to track the transactional progress of a cascade, however, I am no expert on the subject.

#16887 report it
Trejder at 2014/04/09 02:09am
Re: cascade impact

Cascade updates / deletes are 100% handled by database! How would you imagine to catch this on any PHP script or application? PHP script -- that executes SQL query, which in effect causes a cascade update or delete of related records -- doesn't even knows, that such thing like cascade happened. It all goes in the background, handled by your RDBMS!

#15980 report it
Sammaye at 2014/01/07 03:27am
RE: cascade impact

Don't see how any Yii extension can, it isn't tied to the database itself.

#15977 report it
Prashant Thakkar at 2014/01/07 02:24am
cascade impact

Hello,

Just wants to understand how this extension takes care of records that are getting impacted (modified/deleted) due to cascade update/delete.

#15570 report it
vinoth.mohan at 2013/11/22 04:54am
Install is set to false in your config

hi ,

im am receiving this error 'Install is set to false in your config' when clicked the install link.

any idea?

#13748 report it
Bogel at 2013/06/21 01:39pm
Composite primary key

LoggableBehavior.php will issue an error if the model has more than 1 field as primary key. So, I change this line

$log->model_id = $this->Owner->getPrimaryKey();

to be

if (is_array($this->Owner->getPrimaryKey()))
    $modelId = implode(", ", $this->Owner->getPrimaryKey());
else
    $modelId = $this->Owner->getPrimaryKey();
$log->model_id = $modelId;
#12716 report it
Vicer at 2013/04/08 02:59am
audittrail2

for anyone who is viewing this page.. I think it should be mentioned that audittrail2 is available here: http://www.yiiframework.com/extension/audittrail2/

#11675 report it
Anthony Gough at 2013/01/26 06:41am
Great Extension

Easy to install - you did not lie - up and running maybe not 5 minutes but pretty close. I used the migration however I had to create the audit trail table myself however once I did that it was only a matter of adding the logging function to each active record. Great stuff!

#9488 report it
Sammaye at 2012/08/16 06:52am
Ignored and allowed fields

A per the forum, for those who will miss it:

I have actually added the functionality for ignore and allowed lists within the behaviour.

Here is the Github for it: https://github.com/Sammaye/audittrail

To the author: feel free to patch with this Git if it works for you and everything.

#8442 report it
poorroop at 2012/06/04 09:34am
Ignoring fields in a model OR skipping fields to be logged

Hi, I would like to know if i can have a ignore list in the Model so that AuditTrail does not log the fields in the ignore list in that particular model?

#8270 report it
MadSkillsTisdale at 2012/05/22 10:29am
Unsure of the problem

roopeshpdit - Is it possible you are confusing primary key and user id? The user id is simply the id of the user class, it should not change across your project. The primary key is per table, and this is found internally by $this->Owner->getPrimaryKey() and is not something you have to set manually.

#8250 report it
poorroop at 2012/05/21 11:59pm
Have two tables one with PK=id another PK=User_no how can create common audit trail for both

I have 2 tables table 1: PK = id table 2: PK = user_no Presently i am doing this:

my AuditTrailModule.php

public $userClass = "User";
 
    /**
     * @var string the name of the column of the user class that is the primary key. Defaults to "id"
     */ 
    public $userIdColumn = "id";
 
    /**
     * @var string the name of the column of the user class that is the username. Defaults to "username"
     */ 
    public $userNameColumn = "username";

My config main.php file

'modules'=>array(
        'auditTrail'=>array(),

My problem is i can configure the audittrail for both tables I mean i cannot have id and user_no at the same time. is there a way to specify

public $userIdColumn = "id";

for table 1

public $userIdColumn = "user_no";

for table 2

Please let me know if i missing some thing

#8075 report it
maupv99 at 2012/05/08 01:45pm
some error happens , the migration class seems to have some syntax error in version 1.2.0

I'm using mysql, and had the same errors that you mention. Fixed them by adding a data type to the two fields, and adding a prefix index to the text type fields

public function up()
    {
 
        //Create our first version of the audittrail table  
        //Please note that this matches the original creation of the 
        //table from version 1 of the extension. Other migrations will
        //upgrade it from here if we ever need to. This was done so
        //that older versions can still use migrate functionality to upgrade.
        $this->createTable( 'tbl_audit_trail',
            array(
                'id' => 'pk',
                'old_value' => 'text',
                'new_value' => 'text',
                'action' => 'string NOT NULL',
                'model' => 'string NOT NULL',
                'field' => 'string NOT NULL',
                'stamp' => 'datetime NOT NULL',
                'user_id' => 'string',
                'model_id' => 'string NOT NULL',
            )
        );
 
        //Index these bad boys for speedy lookups
        $this->createIndex( 'idx_audit_trail_user_id', 'tbl_audit_trail', 'user_id');
        $this->createIndex( 'idx_audit_trail_model_id', 'tbl_audit_trail', 'model_id');
        $this->createIndex( 'idx_audit_trail_model', 'tbl_audit_trail', 'model');
        $this->createIndex( 'idx_audit_trail_field', 'tbl_audit_trail', 'field');
        $this->createIndex( 'idx_audit_trail_old_value', 'tbl_audit_trail', 'old_value(20)');
        $this->createIndex( 'idx_audit_trail_new_value', 'tbl_audit_trail', 'new_value(20)');
        $this->createIndex( 'idx_audit_trail_action', 'tbl_audit_trail', 'action');
    }
#5594 report it
yiqing95 at 2011/10/23 12:33pm
some error happens , the migration class seems to have some syntax error in version 1.2.0
public function up()
    {
 
        //Create our first version of the audittrail table  
        //Please note that this matches the original creation of the 
        //table from version 1 of the extension. Other migrations will
        //upgrade it from here if we ever need to. This was done so
        //that older versions can still use migrate functionality to upgrade.
        $this->createTable( 'tbl_audit_trail',
            array(
                'id' => 'pk',
                'old_value' => 'text',
                'new_value' => 'text',
                'action' => 'string NOT NULL',
                'model' => 'NOT NULL',
                'field' => 'NOT NULL',
                'stamp' => 'datetime NOT NULL',
                'user_id' => 'string',
                'model_id' => 'string NOT NULL',
            )
        );
 
        //Index these bad boys for speedy lookups
        $this->createIndex( 'idx_audit_trail_user_id', 'tbl_audit_trail', 'user_id');
        $this->createIndex( 'idx_audit_trail_model_id', 'tbl_audit_trail', 'model_id');
        $this->createIndex( 'idx_audit_trail_model', 'tbl_audit_trail', 'model');
        $this->createIndex( 'idx_audit_trail_field', 'tbl_audit_trail', 'field');
        $this->createIndex( 'idx_audit_trail_old_value', 'tbl_audit_trail', 'old_value');
        $this->createIndex( 'idx_audit_trail_new_value', 'tbl_audit_trail', 'new_value');
        $this->createIndex( 'idx_audit_trail_action', 'tbl_audit_trail', 'action');
    }

please check the code ! in mysql is it possible to create index to a text field? i am not sure . the model and field have no dbType .

#5323 report it
Van Damm at 2011/10/04 09:57am
Van Damm

I've left some feedback and an enhancement proposal in the forum thread http://www.yiiframework.com/forum/index.php?/topic/13647-extension-audittrail/page__view__findpost__p__117976 : please review it

#4576 report it
MadSkillsTisdale at 2011/07/21 10:40am
1.2 is out!

I fixed all problems mentioned in these comments for version 1.2. Please try it out!

#4235 report it
Fábio Sales at 2011/06/17 10:49pm
Bug

In the class "LoggableBehavior", please replace "$log=new auditTrail;" by "$log=new AuditTrail;" (lines 65 and 95).

#4234 report it
davi_alexandre at 2011/06/17 10:23pm
include(auditTrail.php): failed to open stream: No such file or directory

There are two typos in behavior/LoggableBehavior.php, at lines 65 and 95. We should have $log = new AuditTrail instead of $log = new auditTrail

#4017 report it
MadSkillsTisdale at 2011/05/27 05:28pm
Feature roll up for 1.2

I am about to finalize version 1.2. If you have any interest in the features and changes that will be included, please check out the forum here and add your voice!

http://www.yiiframework.com/forum/index.php?/topic/13647-extension-audittrail/page__st__40__gopid__97622#entry97622

#3989 report it
migajek at 2011/05/25 08:08am
Bugfix in LoggableBehavior

Hi, see line 20 of LoggableBehavior. Currently it checks whether userid is empty. it should check also if it is numeric (integer), since this kind of data is required by DB.

if(empty($userid) || !is_numeric($userid)) { $userid = 0; }

Leave a comment

Please to leave your comment.

Create extension