cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[Need advice] How to use join and model at once?

Open borisdelev opened this issue 11 years ago • 3 comments

Hi there... I read documentation so many times but... i never understand is it possible to make a query from model, to use model functions but to include more fields from join. Let explain from example:

    $groups = \Models\Articles::query()
        ->where("is_active=1")
        ->order("date DESC")
        ->innerJoin("Models\ArticlesKinds", "k.article_id = Models\Articles.id", "k")
        ->execute();

So - in this case- all its okay. Its works fine. In results i can use function in Articles model like getHowMuchLikes() :) (just for example).

But... if i want to get more colums, from joined table:

    $groups = \Models\Articles::query()
        ->columns(array("Models\Articles.*", "k.kind_id"))
        ->where("is_active=1")
        ->order("date DESC")
        ->innerJoin("Models\ArticlesKinds", "k.article_id = Models\Articles.id", "k")
        ->execute();

...now... i cant use my model functions :( So, i hope u get it what i mean. It is interesting for me, because i use other table for i18n (language variations) for my data - its important and clean way to make dynamic articles with few languages. And if i have a way to do that:

    $groups = \Models\Articles::query()
        ->columns(array("Models\Articles.*", "i18n.*"))
        ->where("is_active=1")
        ->order("date DESC")
        ->join("Models\ArticlesI18n", "i18n.article_id = Models\Articles.id", "i18n")
        ->execute();

...well.. will be happy xD Less queries - faster application.

So thank you. Hope you understand me and sorry for my english skills. Thanks in advice for opinions and recommendations.

borisdelev avatar May 28 '15 20:05 borisdelev

Hi, you can't use the model functions because when you add columns, query returns not a model, but Resultset Simple. When I need to do this, I do like:

$query = $this->getModelsManager()->createBuilder()
    ->innerJoin()
    ->where()
    // .... etc
    ;
$rs = $query->execute();
$return = array();
foreach ($rs as $row) {
    $model = new Model();
    $model->setId($row->id)
           ->setOtherAttribute($row->otherAttribute)
           // ... so on
           ;
    $otherModel = new OtherModel();
    $otherModel->setId($row->idOtherModel)
                 ->setOtherAttribute($row->otherModelAttribute)
                // ... so on
                ;
    // here make a relation if exists, like
    // $model->setOtherModel($otherModel);
    $return[] = $model;
}

Of course, this is a good idea if I know and sure the resultset will have a few rows, because the performance or memory, etc.

I hope this can help you. And same, sorry my bad English, do you speak spanish? I do speak spanish.

gtux avatar May 29 '15 18:05 gtux

Nice idea... didnt try this yet. Thank you for your advice and help. I saw in Phalcon blog that they will make Eager-Loading in PHQL- so soon we will be able to do it easy.

Thank you gtux. (your english is goood :P i am for Bulgaria and spanish- didnt know a word xD

borisdelev avatar May 30 '15 13:05 borisdelev

Hello, Github is only intended to report bugs or discuss new features, to get advise about the usage of the framework you can use the forum: http://forum.phalconphp.com/

andresgutierrez avatar May 30 '15 14:05 andresgutierrez