Code Monkey home page Code Monkey logo

yii2-audittrail's Introduction

yii2-audittrail

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 [
			'sammaye\audittrail\LoggableBehavior'
		];
	}
}

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 [
			'LoggableBehavior' => [
				'class' => 'sammaye\audittrail\LoggableBehavior',
				'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.

Changes

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\audittrail\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.

Migration

There are two ways of doing this:

Resources

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.