vikilaboy/yii2-activerecord-history

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

安装: 13

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 17

类型:yii2-extension

1.1.1 2018-06-04 17:15 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:21:23 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 许可证下发布。