Loading Models with custom query

Hi all,

I wonder if anyone has an idea on how to supply custom model loading Query to MVC\Model objects?

The question is here:


I need to load a dependent model with properties obtained from parent model. I really don't want to go to database again in afterFetch()...



From stackOverflow:

Hmm.. I'd rather leave findFirst behaviour untouched. Is there a way to tell the Phalcon\Mvc\Model() to use my custom query?

The answer doesn't touch findFirst() or did I misunderstand? You could however overwrite Model::findFirst() but I don't think this is the appropriate solution.


It does:

"Then I'd overload model's findFirst() method"

Composing the query is an easy part. But what to do with it later? findFirst() does too many things internally to be messed with.


I don't understaind the goal of using findFirst()?

You need a model and and the related model(s).

$m = Model::findFirst();
$m->otherModel; //related model

This is however lazyloaded, if you need a solution without lazyloading use the Query builder as suggested. You don't need to overload findFirst(), just use the Query builder instead.

load a dependent model with properties obtained from parent model

This can be done using PHQL or the query builder.