Yii 2.0: yii2-audittrail

A Yii2 Beta port of the audittrail2 plugin

Yii2 edition of the audittrail extension.

How to use

To use this extension you can simply add it to the model's behaviours:

use yii\db\ActiveRecord;

class Model extends ActiveRecord
    public function behaviors()
        return [

You can convert the entered behaviour into a key - value structure to define options for the extension for that model:

class Model extends ActiveRecord
    public function behaviors()
        return [
            'LoggableBehaviour' => [
                'class' => 'sammaye\audittrail\LoggableBehaviour',
                'ignored' => ['some_field'], // This ignores fields from a selection of all fields, not needed with allowed
                'allowed' => ['another_field'] // optional, not needed if you use ignore

And that is basically how to setup this extension.


There are a couple of changes.

One of them is how to define global parameters for this extension.

An example of global parameters would be setting you own table name or defining the location of the user model.

To define your own table you can add a parameter to your params.php file called audittrail.table which would take something like {{%audit}}.

To define your own user model location you can add audittrail.model using a value like common\models\User.

The only other change is really to do with the best way to query the audit trail.

The below example is the one from the previous documentation rewritten for Yii2:

use yii\data\ActiveDataProvider;
use sammaye\auditrail\AuditTrail;
use common\models\Title;
use common\models\Product;

$model_ids = array(array($model->id, Title::className()));
foreach($model->products as $id => $product){
    $model_ids[] = array($product->id, Product::className());

$criteria = AuditTrail::find();
$param_id = 0;

// $model_ids is the one you built in your original code
foreach( $model_ids as $id_pair ) {
    $criteria->orWhere('model_id = :id' . $param_id . ' AND model = :model' . $param_id);
        ':id' . $param_id => $id_pair[0], 
        ':model' . $param_id => $id_pair[1]
$criteria->orderBy(['stamp' => SORT_DESC]);

echo yii\grid\GridView::widget([
    'dataProvider' => new ActiveDataProvider([
        'query' => $criteria,
        'pagination' => [
            'pageSize' => 100,
    'columns' => [
            'label' => 'Author',
            'value' => function($model, $index, $widget){
                return $model->user ? $model->user->email : "";
            'attribute' => 'model',
            'value' => function($model, $index, $widget){
                $p = explode('\\', $model->model);
                return end($p);
            'label' => 'field',
            'value' => function($model, $index, $widget){
                return $model->getParent()->getAttributeLabel($model->field);
            'label' => 'Date Changed',
            'value' => function($model, $index, $widget){
                return date("d-m-Y H:i:s", strtotime($model->stamp));
]); ?>

Notice the:

    'attribute' => 'model',
    'value' => function($model, $index, $widget){
        $p = explode('\\', $model->model);
        return end($p);

This allows us to get the name of the model without the namespace part on it.

Currently in this extension I store the fully qualified name of the class to:

  • Take advantage of namespacing to create uniqueness
  • To make it easier to query using the className() function in Objects.


There are two ways of doing this:


Total 10 comments

#19176 report it
Sammaye at 2015/04/07 03:20am
Re: Request for the steps of the extension.

Added something of a tutorial, thanks

#19175 report it
Sukunj Mendpara at 2015/04/06 06:58am
Request for the steps of the extension.

Thanks for the reply.

Can u write the steps to use this extension.

If you can do this then it will be very helpful for the User.

#19174 report it
Sammaye at 2015/04/06 06:35am
Re: Class not found error

Ah I am an idiot, I linked the wrong repo at the top. You are following Yii1 instructions. This is the repo you should be using: https://github.com/Sammaye/yii2-audittrail

#19173 report it
Sukunj Mendpara at 2015/04/06 03:07am
Class not found error.

I am getting error for the class not found.

Can u help me for this error.

Error Notice :

Class application.modules.auditTrail.behaviors.LoggableBehavior does not exist

#19119 report it
Sammaye at 2015/03/21 06:38pm
Re: Migration?

Sorry this took me almost a month!

I don't really use SQL much these days and didn't have an SQL server at hand, however, I have finally done this and fixed it.

I have fully tested the fix and added a note to the readme about migrations.

#19057 report it
Sammaye at 2015/03/04 04:02pm
Re: Migration?

Sorry that was the wrong repo, here is the actual link: https://github.com/Sammaye/yii2-audittrail/issues/6

#19056 report it
Sammaye at 2015/03/04 03:32pm
Re: Migration?

I will look into this. I wrote that code before the migration commands etc were solid on Yii2 so they may have changed.

I have added this as an issue here: https://github.com/Sammaye/yii-audittrail/issues/22 and will get onto it within the next day hopefully.

#19055 report it
casinho at 2015/03/04 03:26pm

I tried to run the migration via console but everytime ist says "No new migration found. Your system is up-to-date".

What`s my mistake? I tried the following command: "yii migrate --migrationPath=C:/User/Username/workspace/vendor/sammaye/yii2-audittrail/migrations"

#16489 report it
Sammaye at 2014/02/27 05:37pm
Re: Looks useful

It is still currently based on that system due to being more of a port rather than a rewrite, yes.

#16488 report it
trond at 2014/02/27 04:39pm
Looks useful

I assume this article is still valid as background info http://www.yiiframework.com/wiki/9/how-to-log-changes-of-activerecords

Leave a comment

Please to leave your comment.

Create extension
No downloadable files yet