感光度 / yii2-changelog-behavior
为yii2模型提供简单变更日志行为及差异高亮显示
v1.1.0
2018-11-26 11:44 UTC
Requires
- qazd/text-diff: ^0.1.1
- yiisoft/yii2: ^2.0
README
为yii2模型提供简单变更日志行为及差异高亮显示
安装
1- 通过composer安装包
composer require sensetivity/yii2-changelog-behavior "*"
2- 运行迁移
yii migrate --migrationPath=@vendor/sensetivity/yii2-changelog-behavior/src/migrations
用法
1- 将 ChangeLogBehavior 添加到任何模型或活动记录
public function behaviors() { return [ ... [ 'class' => Sensetivity\ChangeLog\ChangeLogBehavior::class, 'excludedAttributes' => ['updated_at', 'created_at'], ], ... ]; }
注意:行为仅监视“安全”属性。如果您不想记录其更改,请将其添加到 excludedAttributes 中。
2- 将 ChangeLogListWidget 添加到视图中
echo Sensetivity\ChangeLog\ChangeLogListWidget::widget([ 'model' => $model, ])
3- 添加自定义日志
$model->addCustomLog('hello world!', 'hello_type')
示例
模型 Post
/** * @propertu int id * @property int created_at * @property int updated_at * @property string title * @property int rating */ class Post extends yii\db\ActiveRecord { /** * @inheritdoc */ public function behaviors() { return [ [ 'class' => Sensetivity\ChangeLog\ChangeLogBehavior::class, 'excludedAttributes' => ['created_at','updated_at'], ] ]; } }
视图 post/view.php
use Sensetivity\ChangeLog\ChangeLogListWidget; use app\models\Post; /** * @var Post $model */ echo DetailView::widget([ 'model' => $model, 'attributes' => [ 'id', 'title', 'rating', 'created_at:datetime', 'updated_at:datetime', ], ]); echo ChangeLogListWidget::widget([ 'model' => $model, ]);
历史记录
控制器 PostController
use Sensetivity\ChangeLog\actions\ChangeLogAction; use app\models\Post; /** * {@inheritdoc} */ public function actions() { return [ 'changelog' => [ 'class' => ChangeLogAction::class, 'modelClass' => Page::class, ], ]; }
视图 post/view.php
<?= Html::a('Changelog', ['changelog', 'id' => $model->id], ['class' => 'btn btn-warning']) ?>