Skip to content

lebedyncrs/yii2-attrswatcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Attribute watcher for Yii2

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;
      }
  ]
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages