PHPackages                             sensetivity/yii2-changelog-behavior - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sensetivity/yii2-changelog-behavior

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

sensetivity/yii2-changelog-behavior
===================================

Simple changelog behavior with diff highlight for yii2 models

v1.1.0(7y ago)11.8k↓50%MITPHP

Since Nov 23Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Sensetivity/yii2-changelog-behavior)[ Packagist](https://packagist.org/packages/sensetivity/yii2-changelog-behavior)[ RSS](/packages/sensetivity-yii2-changelog-behavior/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (2)Versions (7)Used By (0)

yii2-changelog-behavior
=======================

[](#yii2-changelog-behavior)

Simple changelog behavior with diff highlight for yii2 models

Installation
------------

[](#installation)

1- Install package via composer:

```
composer require sensetivity/yii2-changelog-behavior "*"

```

2- Run migrations:

```
yii migrate --migrationPath=@vendor/sensetivity/yii2-changelog-behavior/src/migrations

```

Usage
-----

[](#usage)

1- Add *ChangeLogBehavior* to any model or active record:

```
public function behaviors()
{
    return [
        ...
        [
            'class' => Sensetivity\ChangeLog\ChangeLogBehavior::class,
            'excludedAttributes' => ['updated_at', 'created_at'],
        ],
        ...
    ];
}
```

**Attention:** Behavior watches to "safe" attributes only. Add attributes into *excludedAttributes* if you don't want to log its changes.

2- Add *ChangeLogListWidget* to view:

```
 echo Sensetivity\ChangeLog\ChangeLogListWidget::widget([
     'model' => $model,
 ])
```

3- Add custom log:

```
$model->addCustomLog('hello world!', 'hello_type')
```

### Example

[](#example)

Model *Post*

```
/**
 * @propertu int id
 * @property int created_at
 * @property int updated_at
 * @property string title
 * @property int rating
 */
class Post extends yii\db\ActiveRecord {

    /**
     *  @inheritdoc
     */
    public function behaviors()
    {
        return [
            [
                'class' => Sensetivity\ChangeLog\ChangeLogBehavior::class,
                'excludedAttributes' => ['created_at','updated_at'],
            ]
        ];
    }

}
```

View *post/view.php*

```
use Sensetivity\ChangeLog\ChangeLogListWidget;
use app\models\Post;

/**
 *  @var Post $model
 */
echo DetailView::widget([
    'model' => $model,
    'attributes' => [
        'id',
        'title',
        'rating',
        'created_at:datetime',
        'updated_at:datetime',
    ],
]);

echo ChangeLogListWidget::widget([
    'model' => $model,
]);
```

#### History

[](#history)

Controller *PostController*

```
use Sensetivity\ChangeLog\actions\ChangeLogAction;
use app\models\Post;

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'changelog' => [
                'class' => ChangeLogAction::class,
                'modelClass' => Page::class,
            ],
        ];
    }
```

View *post/view.php*

```
