Thanks for that information, Barbery. I took a look a stab at it this morning and came up with the below.
I did neglect to mention that I was using Mongodb backend, and thus using \Phalcon\Mvc\Collection
. After playing around this morning, I found that I can leverage writeAttribute
.
I am doing this so as to make it much simipler to have related models in the system. See below for what I am doing. Feel free to comment if you have a better solution.
Base Model
public function beforeSave()
{
foreach ($this as $key => $value) {
if (substr($key, 0, 1) == "_") {
continue;
}
if (is_subclass_of($value, __CLASS__)) {
$value->save();
$ref = array(
'referencedClass' => $value->getSource(),
'id' => $value->getId()
);
$this->$key = $ref;
}
}
}
public function writeAttribute($name, $value)
{
if (is_array($value) && array_key_exists('referencedClass', $value) && array_key_exists('id', $value) &&
$value['id'] instanceof \MongoId) {
$className = preg_replace("/_/", '\\', $value['referencedClass']);
$oldValue = $value;
$value = call_user_func_array(
$className . '::findFirst',
array(array(
'_id' => $value['id']
))
);
}
return parent::writeAttribute($name, $value);
}
Example usage:
$user = new \Netadmin\Main\Model\User();
$user->fName = "First";
$user->lName = "Last";
$role = new \Netadmin\Main\Model\Role();
$role->role = "Admin";
$user->role = $role;
$user->save();
So, the Role object will be saved into its own collection, and when the user is pulled from the db, thie base model will automatically load that model into the value, reducing duplication.