In model I add behaviors to set actual user on insert:
public function behaviors()
{
return [
[
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'fk_creator',
'updatedByAttribute' => false,
'attributes' => [
ActiveRecord::EVENT_BEFORE_VALIDATE => ['fk_creator']
]
],
];
}
Set actual user to fk_creator at insert, not at update.
Refresh fk_creator at every update.
| Q | A
| ---------------- | ---
| Yii version | 2.0.12
| PHP version | 7.1.12
| Operating system | Windows 10
See details from attributes property:
List of attributes that are to be automatically filled with the value specified via $value. The array keys are the ActiveRecord events upon which the attributes are to be updated, and the array values are the corresponding attribute(s) to be updated. You can use a string to represent a single attribute, or an array to represent a list of attributes. For example,
[ ActiveRecord::EVENT_BEFORE_INSERT => ['attribute1', 'attribute2'], ActiveRecord::EVENT_BEFORE_UPDATE => 'attribute2', ]
In your case, you define when before validate, the attribute fk_creator should be updated. When should be something like
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['fk_creator']
]
@marcelodeandrade Quote from api:
By default, BlameableBehavior will fill the created_by and updated_by attributes with the current user ID when the associated AR object is being inserted; it will fill the updated_by attribute with the current user ID when the AR object is being updated.
So, fk_creator should only be filled at insert, not at update.
You are overrind the default behavior from BlameableBehavior when set the attributes. Change to
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['fk_creator']
]
I see. Thanks for helping!
Most helpful comment
You are overrind the default behavior from BlameableBehavior when set the attributes. Change to