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": "*"
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;
}
]
}