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 ¶
- Unzip auditTrail.zip and move the auditTrail folder to your /protected/modules folder
- Add the following code to your /protected/config/main.php
...
'import'=>array(
...
'application.modules.auditTrail.models.AuditTrail',
...
),
...
'modules'=>array(
...
'auditTrail'=>array(),
...
),
...
- Visit http://yourapp/index?r=auditTrail
- 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!
error
Fatal error: Class 'AuditTrail' not found in domain.com\protected\modules\auditTrail\behaviors\LoggableBehavior.php on line 37
answer
Problem solved
added this code ^^
'application.modules.auditTrail.models.',
'application.modules.auditTrail.components.',
Fixed docs
You got that error because I forget to mention that you should import the audit trail model. I changed the docs. Sorry about that! This is what you need in your main.php config file:
'import'=>array( ... 'application.modules.auditTrail.models.AuditTrail', ... ),
AdminController and DefaultController causing errors...
Having installed the module (unzipping the directory, adding the module to the config file), I visited the local URL to be shown an error saying my install couldn't find 'Controller.php' when instantiating the page (DefaultController, in this case).
This extends 'Controller', so I changed it to extend 'CController' and removed the references to the breadcrumb from the views and it works fine from there on in.
Is there any obvious reason why I couldn't have a local Controller.php?
Controller vs CController
Controller is generated automatically by yiic tool when creating a new web app, so I do not want to overwrite this in this module.
May I ask how you wound up without a Controller class? Are you using an app from scratch or adding this to an existing one?
Postgres Table Creation Script
I hope to have time to change the database creation to use migrations now that Yii supports them. In the meantime, here is a creation script for postgres, contributed by cesarbis on the forums:
CREATE FUNCTION update_stamp() RETURNS trigger LANGUAGE plpgsql AS $$begin new.stamp := now(); return new; end;$$; CREATE TABLE tbl_audit_trail ( id serial NOT NULL CONSTRAINT tbl_audit_trail_pkey PRIMARY KEY, old_value text NOT NULL, new_value text NOT NULL, action character varying(20) NOT NULL, model character varying(255) NOT NULL, field character varying(64) NOT NULL, stamp timestamp without time zone DEFAULT ('now'::text)::timestamp without time zone NOT NULL, user_id integer NOT NULL, model_id character varying(65) NOT NULL ); CREATE INDEX idx_action ON tbl_audit_trail USING btree (action); CREATE INDEX idx_field ON tbl_audit_trail USING btree (field); CREATE INDEX idx_model ON tbl_audit_trail USING btree (model); CREATE INDEX idx_model_id ON tbl_audit_trail USING btree (model_id); CREATE INDEX idx_new_value ON tbl_audit_trail USING btree (new_value); CREATE INDEX idx_old_value ON tbl_audit_trail USING btree (old_value); CREATE INDEX idx_user_id ON tbl_audit_trail USING btree (user_id); CREATE TRIGGER stamp_update BEFORE UPDATE ON tbl_audit_trail FOR EACH ROW EXECUTE PROCEDURE update_stamp();
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;
}
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
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
Bug
In the class "LoggableBehavior", please replace "$log=new auditTrail;" by "$log=new AuditTrail;" (lines 65 and 95).
1.2 is out!
I fixed all problems mentioned in these comments for version 1.2. Please try it out!
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
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 .
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'); }
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
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.
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?
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.
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!
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/
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;
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?
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.
RE: cascade impact
Don't see how any Yii extension can, it isn't tied to the database itself.
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!
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.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.