I have ActiveRecord instance with bunch of behaviors attached.
Inside one of them related models get updated (besides owner). Using updateAttributes() is OK, but I want to trigger few behaviors for updating related models, for example TimestampBehavior, BlameableBehavior, LogBehavior.
If we call save() inside model recursion can occur, and sometimes some behaviors must be excluded.
I came up with following solution, but it fails when attaching behaviors back.
$detachedBehaviors = $this->getDetachedBehaviors();
foreach ($model->behaviors as $key => $behavior) {
if (in_array($behavior::className(), $detachedBehaviors)) {
$model->detachBehavior($key);
}
}
$model->save(false, [$this->attribute]);
$model->attachBehaviors($detachedBehaviors);
Maybe there is better way of doing that?
I see another alternative:
-
Somehow manually trigger needed events of needed behaviors.
-
Use updateAttributes() instead of calling save().
Actually I don’t want to bind this logic to specific behavior and made this available for any behavior which is more flexible. So I guess it will be another behavior.