yii2-audittrail

A Yii2 Beta port of the audittrail2 plugin
3 followers

yii2-audittrail

Note: This is the rename of this extension: yii2-auditrail to match the previous name of it in Yii1.

Yii2 edition of the audittrail 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:

<?php
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);
    $criteria->addParams([
        ':id' . $param_id => $id_pair[0], 
        ':model' . $param_id => $id_pair[1]
    ]);
    $param_id++;
}
$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);
            }
        ],
        'model_id',
        'action',
        [
            'label' => 'field',
            'value' => function($model, $index, $widget){
                return $model->getParent()->getAttributeLabel($model->field);
            }
        ],
        'old_value',
        'new_value',
        [
            '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.

Todo:

  • Test the migration

Resources:

Total 2 comments

#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
Downloads
No downloadable files yet