PHPackages                             yii2tech/model-change - 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. yii2tech/model-change

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

yii2tech/model-change
=====================

Provides model data and state change tracking for Yii2

1.0.0(9y ago)125.2k3BSD-3-ClausePHP

Since Apr 19Pushed 6y ago2 watchersCompare

[ Source](https://github.com/yii2tech/model-change)[ Packagist](https://packagist.org/packages/yii2tech/model-change)[ RSS](/packages/yii2tech-model-change/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

 [ ![](https://avatars2.githubusercontent.com/u/12951949) ](https://github.com/yii2tech)

Model Change Tracking Extension for Yii2
========================================

[](#model-change-tracking-extension-for-yii2)

This extension provides Yii2 model data and state change tracking.

For license information check the [LICENSE](LICENSE.md)-file.

[![Latest Stable Version](https://camo.githubusercontent.com/72bf79b25a2407ab2ed505aa6fbd9d603ccf89dde035a29c7b67339d5e796ad8/68747470733a2f2f706f7365722e707567782e6f72672f79696932746563682f6d6f64656c2d6368616e67652f762f737461626c652e706e67)](https://packagist.org/packages/yii2tech/model-change)[![Total Downloads](https://camo.githubusercontent.com/d1bdfb1a15112ecff25994a09c6ed4940f5982d98ca2a84198e475a0c7f9c702/68747470733a2f2f706f7365722e707567782e6f72672f79696932746563682f6d6f64656c2d6368616e67652f646f776e6c6f6164732e706e67)](https://packagist.org/packages/yii2tech/model-change)[![Build Status](https://camo.githubusercontent.com/7c3598cd9c3b7576a1d9f5b18575ae8ade1e0eff01723444d65e07ec269c4e09/68747470733a2f2f7472617669732d63692e6f72672f79696932746563682f6d6f64656c2d6368616e67652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/yii2tech/model-change)

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist yii2tech/model-change

```

or add

```
"yii2tech/model-change": "*"
```

to the require section of your composer.json.

Usage
-----

[](#usage)

This extension provides Yii2 model data and state change tracking. It provides solution, which works around the model classes, allowing their events tracking from external, allowing enabling or disabling it at will.

Imagine following use case: we composing complex web pages, which content is based on database records. So we have an administration panel, which provides setup for particular page content parts, like 'header', 'footer' etc, as well as custom pages and main menu items. In order to keep high performance we widely use cache for different pages and page parts, avoiding regular database queries for page contents. However, once some record is changed from admin panel the cache should be invalidated, so changes may actually appear at the side. But it is not very practical to clear entire cache per each content database record change as system administrator may edit several records during single user session and only after he consider all changes are done cache should be cleared. Thus instead of clearing cache we want simply show some notification to the user at web interface, which should remind him that cache should be cleared before changes will appear at the main site.

It is not good to place such functionality inside the model classes using model events or behaviors as functionality will affect only administration panel and should not consume resources at main or console application. This means that model event handlers, which respond model saving and deletion, should be assigned dynamically from outside.

Controller Filter
------------------

[](#controller-filter-)

The use case described above can be solved using \[\[\\yii2tech\\modelchange\\ModelChangeFilter\]\]. As a filter it can be attached either to the controller or module (including application itself).

Controller configuration example:

```
class PageController extends \yii\web\Controller
{
    public function behaviors()
    {
        return [
            'modelChange' => [
                'class' => ModelChangeFilter::className(),
                'except' => [
                    'index',
                    'view'
                ],
                'modelClasses' => [
                    'app\models\Page'
                ],
                'afterModelChange' => function ($event) {
                    Yii::$app->getSession()->set('cacheFlushRequired', true);
                },
            ],
        ];
    }

    // ...
}
```

Now, once `app\models\Page` model is saved during some `PageController` action run, the session flag 'cacheFlushRequired' will be set. This flag should be processed somewhere at administration page layout, like following:

```

        You need to clear cache
