tigrov / yii2-pgsql-audit
仅适用于 Yii2 的 ActiveRecord 模型的 PostgreSQL 审计工具
1.3.5
2019-08-15 08:37 UTC
Requires
- php: >=5.5.0
- tigrov/yii2-pgsql-enum: ~1.0
- yiisoft/yii2: ^2.0.14.2
README
仅适用于 Yii2 的 ActiveRecord 模型的 PostgreSQL 审计工具。
该扩展用作 ActiveRecord 模型的一个行为。它会存储模型的所有更改,并允许您审查和撤销更改。
限制
仅适用于 PostgreSQL。
扩展针对 integer
类型的 \Yii::$app->user->id
进行优化
以及具有 integer
类型 主键 的 ActiveRecord
模型。
如果您有不同的 integer
类型,可以继承类并做出必要的更改。还需要更改审计表的模式。
- 自 1.2.0 版起需要 PHP >= 5.5
- 自 1.3.0 版起需要 Yii >= 2.0.14.2
依赖
该扩展依赖于以下扩展
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist tigrov/yii2-pgsql-audit
或在您的 composer.json
文件的 require 部分添加
"tigrov/yii2-pgsql-audit": "~1.0"
到
配置
扩展安装后,在 config.php
中配置迁移
return [ // ... 'controllerMap' => [ 'migrate' => [ 'class' => 'yii\console\controllers\MigrationController', ], ], // ... ];
并应用迁移
yii migrate
用法
将行为 AuditableBehavior
添加到模型类。
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