bupy7/yii2-activerecord-history

此包已被弃用且不再维护。未建议替代包。

此扩展为ActiveRecord模型添加变更存储历史。

安装数: 70,260

依赖者: 0

建议者: 0

安全: 0

星标: 26

关注者: 6

分支: 16

开放问题: 1

类型:yii2-extension

1.2.0 2019-07-10 10:13 UTC

This package is auto-updated.

Last update: 2021-04-10 13:50:12 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Build Status Coverage Status

此扩展为ActiveRecord模型添加变更存储历史。

扩展能够追踪模型变更并将其保存到存储中。仅允许存储到数据库。所有变更都保存在数据库表中。

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一

php composer.phar require --prefer-dist bupy7/yii2-activerecord-history "*"

或者将以下内容添加到你的 composer.json 文件的 require 部分。

"bupy7/yii2-activerecord-history": "*"

使用

使用方法

注意!如果使用 andanced 模板,则必须在 common/config/main.php 中配置模块。如果使用 basic 模板,则必须在 config/web.phpconfig/console.php 中添加配置。

将模块注册到配置文件中

'modules' => [
    'arhistory' => [
        'class' => 'bupy7\activerecord\history\Module',
    ],
],

模块名称可以是任何名称(例如:history、custom-name 等)。

在配置文件中添加模块到启动部分

'bootstrap' => ['arhistory'],

运行迁移

php ./yii migrate/up --migrationPath=@bupy7/activerecord/history/migrations

将行为附加到模型上

use bupy7\activerecord\history\behaviors\History as HistoryBehavior;

$model->attachBehavior('arHistory', HistoryBehavior::className());

配置

模块配置

'modules' => [
    'arhistory' => [
        'class' => 'bupy7\activerecord\history\Module',
        'tableName' => '{{%arhistory}}', // table name of saving changes of model
        'storage' => 'bupy7\activerecord\history\storages\Database', // class name of storage for saving history of active record model
        'db' => 'db', // database connection component config or name
        'user' => 'user', // authentication component config or name
    ],
],

行为配置

use bupy7\activerecord\history\behaviors\History as HistoryBehavior;

$model->attachBehavior('arHistory', [
    'class' => HistoryBehavior::className(),
    // allowed events list than are monitored and saved in storage.
    'allowEvents' => [
        HistoryBehavior::EVENT_INSERT,
        HistoryBehavior::EVENT_UPDATE,
        HistoryBehavior::EVENT_DELETE,
    ],
    // list of attributes which not need track at updating. Apply only for `HistoryBehavior::EVENT_UPDATE`.
    'skipAttributes' => [
        'name_of_attribute_1',
        'name_of_attribute_2',
    ],
    // list of custom attributes which which are a pair of `key`=>`value` where `key` is attribute name and
    // `value` it anonymous callback function of attribute. Function will be apply for old and value information data.
    // Apply only for `HistoryBehavior::EVENT_UPDATE`.
    'customAttributes' => [
        'name_of_attribute_3' => function($event, $isNewValue) {
            if ($isNewValue) {
                return $event->sender->name_of_attribute_3; 
            }
            return $event->changedAttributes['name_of_attribute_3'];
        },
    ],
]);

许可证

yii2-activerecord-history 在 BSD 3-Clause 许可下发布。