sateler/yii2-changelog

Yii2 behavior for storing model/table changelogs

Installs: 1 026

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 3

Forks: 0

Open Issues: 1

Type:yii2-extension

1.0.6 2020-11-23 16:00 UTC

This package is auto-updated.

Last update: 2022-11-23 20:16:28 UTC


README

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist sateler/yii2-changelog "^1.0"

or add

"sateler/yii2-changelog": "^1.0"

to the require section of your composer.json file.

Once the extension is installed, add namespace to console config and run the required migration:

return [
    'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationNamespaces' => [
                'sateler\changelog\migrations',
            ],
        ],
    ],
];

./yii2 migrate

Usage

Once installed, enable changelog for selected models by adding the following config to the model:

public function behaviors()
{
    return [
        [
            'class' => \sateler\changelog\ChangeLogBehavior::className(),
            'ignore' => [], // ignore changes on listed columns
        ],
        ...
    ];
}

You can review the model changelog at http://hostname/changelog/, or you can code your own controller by using sateler\changelog\models\Changelog. If you want to 'see' the changelogs, add the following configuration and go to http://hostname/changelog/:

return [
    'controllerMap' => [
        'changelog' => [
            'class' => 'sateler\changelog\controllers\ChangelogController',
            'viewPath' => '@vendor/sateler/yii2-changelog/views/changelog',
            // Optional: if set, it's used in the views to create the html link for the record.
            'urlCreator' => function ($table_name, $row_id) {
                $table_name = \yii\helpers\Html::encode(str_replace('_', '-', $table_name));
                return yii\helpers\Url::to(["$table_name/view", 'id' => $row_id]);
            },
        ]
    ],
];

Or you can code your own controller by using the sateler\changelog\models\Changelog model.