PHP code example of tigrov / yii2-pgsql-audit

1. Go to this page and download the library: Download tigrov/yii2-pgsql-audit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tigrov / yii2-pgsql-audit example snippets


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

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

$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