Is there a way to access related models from event methods like beforeSave()? For example, I'd like to access (read) a piece of data from a parent model, like this:
<?php
class ParentEntity extends \Phalcon\Mvc\Model {
public $some_property;
public function getSomeProperty() {
return $this->some_property;
}
...
}
And in a related child model:
<?php
class ChildEntity extends \Phalcon\Mvc\Model {
public function beforeSave() {
$parentSomeProperty = $this->ParentEntity->getSomeProperty();
// Do some calculations involving the parent property and some properties within the ChildEntity, and then save the result to a child property for saving with the child
}
...
}
If I try to do this, Phalcon throws an exception about not being able to load the ParentEntity model. I can get what I want by using a ParentEntity::findFirst()
and using the related ID since I've already saved the parent record at that point. However, whenever I get to a case where I haven't saved the record beforehand, I'm going to need a solution to this. It seems like Phalcon should let you access the related models during events. I can see a lot of different applications for that. Please let me know if there is a way to accomplish this.
I didn't write in the relations code, but that's assumed that I've set up the relations with Phalcon in the initialize()
method.