This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Audit for ActiveRecord models for Yii2, for PostgreSQL only

Installs: 47

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 1

Open Issues: 0


1.3.5 2019-08-15 08:37 UTC

This package is auto-updated.

Last update: 2021-05-15 13:10:58 UTC


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


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 >=


The extension depends on follow extensions:


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.


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

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

and apply migrations:

yii migrate


Add the behavior AuditableBehavior to a model class.

class Model extends \yii\db\ActiveRecord
    public function behaviors()
        return [

Some examples how you can use it:

$model = new Model;
$model->value = 'a value';

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

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

$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