sammaye/yii2-audittrail
Composer 安装命令:
composer require sammaye/yii2-audittrail
包简介
A port of audit trail
README 文档
README
Yii2 edition of the audittrail extension.
Installation
The preferred way to install this extension is through composer.
php composer.phar require sammaye/yii2-audittrail
Migration
There are two ways of doing this:
- Copy the migration file (within the
migrationsfolder) to yourconsole/migrationsfolder - Run
php yii migrate --migrationPath=@vendor/sammaye/yii2-audittrail/migrations
How to use
To use this extension you can simply add it to the model's behaviours:
use yii\db\ActiveRecord; class Model extends ActiveRecord { public function behaviors() { return [ 'sammaye\audittrail\LoggableBehavior' ]; } }
You can convert the entered behaviour into a key - value structure to define options for the extension for that model:
class Model extends ActiveRecord { public function behaviors() { return [ 'LoggableBehavior' => [ 'class' => 'sammaye\audittrail\LoggableBehavior', 'ignored' => ['some_field'], // This ignores fields from a selection of all fields, not needed with allowed 'allowed' => ['another_field'] // optional, not needed if you use ignore ] ]; } }
And that is basically how to setup this extension.
Changes
There are a couple of changes.
One of them is how to define global parameters for this extension.
An example of global parameters would be setting you own table name or defining the location of the user model.
To define your own table you can add a parameter to your params.php file called audittrail.table which would take something like {{%audit}}.
To define your own user model location you can add audittrail.model using a value like common\models\User.
The only other change is really to do with the best way to query the audit trail.
The below example is the one from the previous documentation rewritten for Yii2:
<?php use yii\data\ActiveDataProvider; use sammaye\audittrail\AuditTrail; use common\models\Title; use common\models\Product; $model_ids = array(array($model->id, Title::className())); foreach($model->products as $id => $product){ $model_ids[] = array($product->id, Product::className()); } $criteria = AuditTrail::find(); $param_id = 0; // $model_ids is the one you built in your original code foreach( $model_ids as $id_pair ) { $criteria->orWhere('model_id = :id' . $param_id . ' AND model = :model' . $param_id); $criteria->addParams([ ':id' . $param_id => $id_pair[0], ':model' . $param_id => $id_pair[1] ]); $param_id++; } $criteria->orderBy(['stamp' => SORT_DESC]); echo yii\grid\GridView::widget([ 'dataProvider' => new ActiveDataProvider([ 'query' => $criteria, 'pagination' => [ 'pageSize' => 100, ] ]), 'columns' => [ [ 'label' => 'Author', 'value' => function($model, $index, $widget){ return $model->user ? $model->user->email : ""; } ], [ 'attribute' => 'model', 'value' => function($model, $index, $widget){ $p = explode('\\', $model->model); return end($p); } ], 'model_id', 'action', [ 'label' => 'field', 'value' => function($model, $index, $widget){ return $model->getParent()->getAttributeLabel($model->field); } ], 'old_value', 'new_value', [ 'label' => 'Date Changed', 'value' => function($model, $index, $widget){ return date("d-m-Y H:i:s", strtotime($model->stamp)); } ] ] ]); ?>
Notice the:
[
'attribute' => 'model',
'value' => function($model, $index, $widget){
$p = explode('\\', $model->model);
return end($p);
}
],
This allows us to get the name of the model without the namespace part on it.
Currently in this extension I store the fully qualified name of the class to:
- Take advantage of namespacing to create uniqueness
- To make it easier to query using the
className()function in Objects.
Resources
sammaye/yii2-audittrail 适用场景与选型建议
sammaye/yii2-audittrail 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 416.91k 次下载、GitHub Stars 达 39, 最近一次更新时间为 2014 年 02 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「extension」 「Audit」 「trail」 「yii2」 「audit trail」 「audittrail」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sammaye/yii2-audittrail 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sammaye/yii2-audittrail 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sammaye/yii2-audittrail 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Log adding/updating/deleting of elements
Doctrine ORM provider for the auditor audit-log library.
A custom URL rule class for Yii 2 which allows to create translated URL rules
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
PB Web Media Audit Bundle for Symfony
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
统计信息
- 总下载量: 416.91k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 40
- 点击次数: 27
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-02-26