tigrov/yii2-pgsql-audit

Audit for ActiveRecord models for Yii2, for PostgreSQL only

Installs: 49

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 1

Open Issues: 0

Type:yii2-extension

1.3.5 2019-08-15 08:37 UTC

This package is auto-updated.

Last update: 2024-10-15 20:44:15 UTC


README

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