We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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:

https://stackoverflow.com/questions/21715835/loading-models-with-inner-join

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()...

Thanks!



2.6k

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.



51.3k

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.



2.6k

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.