To use AttributeBehavior, configure the [[attributes]] property which should specify the list of attributes
that need to be updated and the corresponding events that should trigger the update. Then configure the
[[value]] property with a PHP callable whose return value will be used to assign to the current attribute(s).
For example,
php
use yii\behaviors\AttributeBehavior;
public function behaviors()
{
return [
[
'class' => AttributeBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'attribute1',
ActiveRecord::EVENT_BEFORE_UPDATE => 'attribute2',
],
'value' => function ($event) {
return 'some value';
},
],
];
}
Because attribute values will be set automatically by this behavior, they are usually not user input and should therefore
not be validated, i.e. they should not appear in the [[\yii\base\Model::rules()|rules()]] method of the model.