How to set hydration mode of related models

Hi all,

I have 1 model, MediaRequest, that has a relationship set up with a 2nd model, File, with the alias "Files".

I want to display all the files in my view. To do that I can just write in my controller:

<?PHP
$this->view->files = $this->MediaRequest->Files

However, $this->view->files will then be an array of fully capable File model objects. I don't need that - I just want plain objects. The documentation shows how to set the hydrate mode when directly creating a list of models using find(), but I'm unsure how or when to set the hydration mode when I ask for a list of related models.



81.8k

Hydration modes are set on resultsets, since getting the related objects returns a resultset you can the hydration mode there:

$this->view->files = $this->MediaRequest->setHydrateMode(Resultset::HYDRATE_OBJECTS);

Thanks. At least I was close.

Your code example is a bit off - maybe your fault, maybe mine for not explaining my situation enough. The actual code that worked was:

$this->view->Files = $this->MediaRequest->Files->setHydrateMode(\Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS)

This also works (and is more intuitive code in my opinion, as "setHydrateMode" doesn't sound like it should return anything)

$this->view->Files = $this->MediaRequest->getFiles(['hydration' => \Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS]);