I've made a behavior, that updates related entities according to some programming logic. I would like to do this inside an event, that is called during save() process, because I do some validation as well and I need to cancel save on errors. But apparently related values are locked on save, and any changes I do are discarded.
public function beforeSave() {
$related = new Related();
$related->name = "related";
$this->related = array($related);
echo "Called";
}
Works:
$model->beforeSave();
// or $model->getModelsManager()->notifyEvent("beforeSave", $model);
$model->save();
Doesn't work:
$model->save();
I see "Called" in both cases, so method is executed. The model itself is getting saved, but the related object is not saved. Is there any way to change related entities during save process?