lebedyncrs/yii2-attrswatcher

Sometimes you want on change some attribute set value to another attribute. This extention provide that.

dev-master 2016-08-08 07:20 UTC

This package is not auto-updated.

Last update: 2024-09-09 12:04:08 UTC


README

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist lebedyncrs/yii2-attrswatcher

or add

"lebedyncrs/yii2-attrswatcher": "*"

to the require section of your composer.json. Usage

Sometimes you want on change some attribute set value to another attribute. This behavior provide that.
Attach behavior to model:

public function behavior(){
  [
    'class' => AttrsWatcherBehavior::className(),
    'attributes' => [
          'Status' => [
              AttrsWatcherBehavior::ATTRIBUTE => 'ClosedOn',
              AttrsWatcherBehavior::FROM => null
              AttrsWatcherBehavior::TO => 1
            ],
            'Rel_DealStage' => [
                   AttrsWatcherBehavior::ATTRIBUTE => 'LastStageChange',
            ]
      ]
  ]
}

With this configuration behavior set current timestamp to ClosedOn attribute when Status will be changed from null to 1. When Rel_DealStage will changed LastStageChange will containt current timestamp.

public function behavior(){
  [
    'class' => AttrsWatcherBehavior::className(),
    'attributes' => [
          'Status' => [
              AttrsWatcherBehavior::ATTRIBUTE => 'ClosedOn',
              AttrsWatcherBehavior::FROM => null
              AttrsWatcherBehavior::TO => 1
            ],
            'Rel_DealStage' => [
                   AttrsWatcherBehavior::ATTRIBUTE => 'LastStageChange',
            ]
      ],
      'values' => [
          'ClosedOn' => 'my own value',
          'LastStageChange' => 'my own value'
      ]
  ]
}

As you can see from above example you can set own value for each attribute.
Also you can change default value for all attributes:

public function behavior(){
  [
    'class' => AttrsWatcherBehavior::className(),
    'attributes' => [
          'Status' => [
              AttrsWatcherBehavior::ATTRIBUTE => 'ClosedOn',
          ]
      ],
      'values' => function(){
        return time()-100;
      }
  ]
}