Attribute watcher for Yii2
Installation
The preferred way to install this extension is through composer., (*1)
Either run, (*2)
php composer.phar require --prefer-dist lebedyncrs/yii2-attrswatcher
or add, (*3)
"lebedyncrs/yii2-attrswatcher": "*"
to the require section of your composer.json., (*4)
Usage
Sometimes you want on change some attribute set value to another attribute. This behavior provide that.
Attach behavior to model:, (*5)
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., (*6)
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:, (*7)
public function behavior(){
[
'class' => AttrsWatcherBehavior::className(),
'attributes' => [
'Status' => [
AttrsWatcherBehavior::ATTRIBUTE => 'ClosedOn',
]
],
'values' => function(){
return time()-100;
}
]
}