GithubHelp home page GithubHelp logo

tigrov / yii2-pgsql-audit Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 1.0 27 KB

Audit for ActiveRecord models for Yii2, for PostgreSQL only

License: MIT License

PHP 100.00%
audit pgsql postgres postgresql yii2-extension

yii2-pgsql-audit's Introduction

yii2-pgsql-audit

Audit for ActiveRecord models for Yii2, for PostgreSQL only.

The extension is used as a behavior for an ActiveRecord model. It will store all changes for the model and gives you possibility to review the changes and revert them.

Latest Stable Version Build Status

Limitation

It is for PostgreSQL only.

The extension optimized for integer type of \Yii::$app->user->id
and for ActiveRecord models with integer type of primary key.

If you have different of integer types, you can inherit the classes and make the necessary changes. Also you need to make changes in the audit table schema.

  • Since 1.2.0 requires PHP >= 5.5
  • Since 1.3.0 requires Yii >= 2.0.14.2

Dependents

The extension depends on follow extensions:

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist tigrov/yii2-pgsql-audit

or add

"tigrov/yii2-pgsql-audit": "~1.0"

to the require section of your composer.json file.

Configuration

Once the extension is installed, configure migrations in config.php:

return [
    // ...
    'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrationController',
        ],
    ],
    // ...
];

and apply migrations:

yii migrate

Usage

Add the behavior AuditableBehavior to a model class.

class Model extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            AuditableBehavior::class,
        ];
    }
}

Some examples how you can use it:

$model = new Model;
$model->value = 'a value';
$model->save();

$model->createdAt; // created date and time
$model->createdBy; // instance of \Yii::$app->user->identityClass

// then update it
$model->value = 'new value';
$model->save();

$model->updatedAt; // updated date and time
$model->updatedBy; // instance of \Yii::$app->user->identityClass

// additional features
$model->firstAudit; // \tigrov\pgsql\audit\Audit
$model->lastAudit; // \tigrov\pgsql\audit\Audit

$model->lastAudit->model; // ActiveRecord
$model->lastAudit->user; // instance of \Yii::$app->user->identityClass

$model->lastAudit->revert(); // revert the last changes
$model->firstAudit->revert(); // revert to the first model version

Audit::findByModel($model); // ActiveQuery, to get audit records for the model
Audit::findByUserId($userId); // ActiveQuery, to get audit records for a user

License

MIT

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.