/yii2-activerecord-history

The extension adds storage history of changes to the AR model

Primary LanguagePHPBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Yii2-ActiveRecord-History Extension for Yii 2

This extension adds storage history of changes to the AR model

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require nhkey/yii2-activerecord-history "*"

or add

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

to the require section of your composer.json.

If you are using DBManager as Manager, you need to run

php yii migrate --migrationPath=@vendor/nhkey/yii2-activerecord-history/migrations

Usage

If the property is not specified, the default manager is DBManager. In the extension is two managers: DBManager and FileManager. You can extend the class BaseManager.

1) As extend the class ActiveRecord

To use this extension, simply change the parent class from \yii\db\ActiveRecord to \nhkey\arh\ActiveRecordHistory

For example:

    class MyClass extends ActiveRecord

change to

    class MyClass extends ActiveRecordHistory

The model will have private property $_historyManager, which replies with a call for the Manager.

Example 1

FileManager:

    class MyClass extends \nhkey\arh\ActiveRecordHistory
    {
        $_historyManager = '\nhkey\arh\managers\FileManager'
        $_optionsHistoryManager = [
            'filename' => '/home/user/MyClass.log',
            ];
        ...
    }

Example 2

DBManager:

    class MyClass extends \nhkey\arh\ActiveRecordHistory
    {
        ...
    }

2) As behavior

Easy way:

    class MyClass extends ActiveRecord {
     public function behaviors(){
            return [
                'history' => [
                    'class' => \nhkey\arh\ActiveRecordHistoryBehavior::className(),
                ],
                ...
            ];
        }

If you want use not default manager or set options for manager, you may use this construction:

    class MyClass extends ActiveRecord {
     public function behaviors(){
            return [
                'history' => [
                    'class' => \nhkey\arh\ActiveRecordHistoryBehavior::className(),
                    'manager' => 'ManagerName',
                    'managerOptions' => [
                        ...
                    ],
                ],
                ...
            ];
        }

Example 1

FileManager:

    class MyClass extends ActiveRecord {
     public function behaviors(){
            return [
                'history' => [
                    'class' => \nhkey\arh\ActiveRecordHistoryBehavior::className(),
                    'manager' => '\nhkey\arh\managers\FileManager',
                    'managerOptions' => [
                        'filePath' => '/home/logs/',
                        'isGenerateFilename' => true
                    ],
                ],
                ...
            ];
        }

or

    class MyClass extends ActiveRecord {
     public function behaviors(){
            return [
                'history' => [
                    'class' => \nhkey\arh\ActiveRecordHistoryBehavior::className(),
                    'manager' => '\nhkey\arh\managers\FileManager',
                    'managerOptions' => [
                        'filename' => '/home/user/logs_app/MyClass.log',
                    ],
                ],
                ...
            ];
        }

Example 2

DBManager:

    class MyClass extends ActiveRecord {
     public function behaviors(){
            return [
                'history' => [
                    'class' => \nhkey\arh\ActiveRecordHistoryBehavior::className(),
                    'manager' => '\nhkey\arh\managers\DBManager',
                    'managerOptions' => [
                        'tableName' => 'MyLogTable',
                    ],
                ],
                ...
            ];
        }

Credits

Author: Mikhail Mikhalev

Email: mail@mikhailmikhalev.ru