rmrevin / yii2-changelog
为 Yii 2 框架的 ActiveRecord 变更日志扩展
1.0.1
2016-10-05 08:51 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: 2.0.*
Requires (Dev)
- yiisoft/yii2-debug: 2.0.*
This package is not auto-updated.
Last update: 2024-09-14 18:20:38 UTC
README
此扩展提供变更日志功能。
有关许可信息,请参阅LICENSE文件。
支持
安装
安装此扩展的首选方式是通过composer。
运行以下命令之一:
composer require "rmrevin/yii2-changelog:~1.0"
或者
"rmrevin/yii2-changelog": "~1.0",
将以下内容添加到您的composer.json
文件的require
部分。
执行迁移
php yii migrate --migrationPath=@rmrevin/yii/changelog/migrations
使用方法
要查看历史记录,您可以使用调试的特殊面板。或者在自己的管理面板中创建一个部分来查看数据。
要启用调试面板,在模块配置调试中添加以下代码。
'modules' => [ // ... 'debug' => [ 'class' => yii\debug\Module::className(), 'panels' => [ rmrevin\yii\changelog\debug\panels\ChangelogPanel::class, ], ], ],
对于您想跟踪变更的ActiveRecord
模型,您必须实现接口rmrevin\yii\changelog\interfaces\LoggableInterface
并添加rmrevin\yii\changelog\behaviors\ChangelogBehavior
行为。
示例
<?php use rmrevin\yii\changelog\interfaces\LoggableInterface; use rmrevin\yii\changelog\behaviors\ChangelogBehavior; use yii\db\ActiveRecord; /** * Class ShopItem * * @property integer $id * @property integer $number * @property string $title * @property integer $created_at * @property integer $updated_at * @property integer $synchronized_at */ class ShopItem extends ActiveRecord implements LoggableInterface { /** * @inheritdoc */ public function __toString() { return sprintf('[%s] %s', $this->number, $this->title); } /** * @inheritdoc */ public function behaviors() { return [ // ... [ 'class' => ChangelogBehavior::class, 'ignoreAttributes' => [ // these attributes are not tracked 'updated_at', 'synchronized_at', ], ], ]; } }
完成
现在,当您尝试在{{%changelog}}
表中创建、修改或删除ShopItem
模型的实例时,将记录相关数据。