九英寸尼克 / yii2-audit
扩展 Yii 2 PHP 框架,允许跟踪和查看模型的变更历史。
v1.0
2015-09-07 16:18 UTC
Requires
Requires (Dev)
- phpunit/phpunit: >=3.8
This package is not auto-updated.
Last update: 2024-09-14 17:42:46 UTC
README
扩展 Yii 2 PHP 框架,允许跟踪和查看模型的变更历史。
使用触发器跟踪变更,但在使用不受支持的数据库时,模型事件也应作为回退。
提供
- 一个模型行为
- 加载较旧的模型版本
- 临时禁用跟踪
- 一个命令来管理和验证审计数据库对象
- 一个控制器操作,用于查看模型变更历史
安装
通过 composer 安装
composer require nineinchnick/yii2-audit
警告!如果从备份中恢复数据库,则有必要使用以下查询重新编号 audits.logged_actions.relation_id
列中的表 oids
UPDATE audits.logged_actions SET relation_id = (schema_name || '.' || table_name)::regclass::oid;
记录变更
在 Blameable 和 Timestamp 行为之后将行为附加到模型
use yii\behaviors\BlameableBehavior; use yii\behaviors\TimestampBehavior; use nineinchnick\audit\behaviors\TrackableBehavior; public function behaviors() { return [ BlameableBehavior::className(), TimestampBehavior::className(), [ 'class' => TrackableBehavior::className(), 'mode' => TrackableBehavior::MODE_TRIGGER, ], ]; }
如果使用触发模式,则运行生成迁移的命令,这将创建或更新数据库对象,如触发器和审计表。
首先,在控制台配置中配置控制器
'controllerMap' => [ 'audit' => [ 'class' => 'nineinchnick\audit\console\AuditController', ], ], // .... rest of configuration
然后运行以下命令
./yii audit/migration --modelName=AR_MODEL_CLASS
其中 AR_MODEL_CLASS
是您的模型类名。
显示变更
要查看变更历史,请使用应用程序配置中提供的模块
'modules' => [ 'audit' => [ 'class' => 'nineinchnick\audit\Module', ], // .... other modules ],
示例配置
'audit' => [ 'class' => 'nineinchnick\audit\Module', 'tables' => [ 'orders' => [ 'model' => 'netis\orders\models\Order', 'hiddenColumns' => ['id', 'created_on', 'author_id'], 'updateSkip' => ['updated_on', 'editor_id'], 'relations' => [ 'editor' => [ 'type' => 'LEFT JOIN', 'table' => '{{%users}}', 'on' => 'editor_id = u.id', 'alias' => 'u', 'representive_columns' => 'username', 'label' => Yii::t('models', 'Editor'), ], ] ], ], 'filters' => [ 'dateFrom' => [ 'format' => 'date', 'attribute' => 'operation_date', 'widgetClass' => 'omnilight\widgets\DatePicker', 'options' => ['class' => 'form-control'], 'dateFormat' => 'yyyy-MM-dd', 'rules' => [ [ 'validator' => 'date', 'options' => ['format' => 'Y-m-d'], ] ], 'criteria' => [ 'operator' => '>=', ], ], 'dateTo' => [ 'format' => 'date', 'attribute' => 'operation_date', 'widgetClass' => 'omnilight\widgets\DatePicker', 'options' => ['class' => 'form-control'], 'dateFormat' => 'yyyy-MM-dd', 'rules' => [ [ 'validator' => 'date', 'options' => ['format' => 'Y-m-d'], ] ], 'criteria' => [ 'operator' => '<=', ], ], ], ],