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.



97.7k

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]);


805
edited Jan '20

Hi All!

I know it's a bit old discussion, but any help on this would be appreciated!

I have the same (more or less) question. I have 2 models (League::class and Seasons::class), connected with

$this->hasMany( 'league_id', Seasons::class, 'season_league_id', [ 'alias' => 'seasoninfo', 'hydration' => \Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS ] );

When I try to use (in my controller) something like that

$this->view->setVar('leaguesList',Leagues::find());

I can use later in my template the relation 'seasoninfo', but if I use the find like this

$this->view->setVar('leaguesList',Leagues::find(['hydration' => Resultset::HYDRATE_ARRAYS]));

I cannot use later the 'seasoninfo'. The for / loop statements in volt don't get any relation.

Thank you for any help!!!