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

Can't get relation when use custom columns

I try to get the relation but when i use custom columns it doesn't work, for example

MODEL

    public function initialize(){
        $this->hasManyToMany(
            "id",
            'Models\Movies\MoviesGenres',
            "movie_id",
            "genre_id",
            'Models\Movies\Genres',
            "id",
            ['alias' => 'genres']
        );
    }

CODE

// THIS WORKS
$movies = Movies::find(["limit" => "10"]);
foreach ($movies as $movie){
    var_dump($movies->genres); // WORKS
}

// THIS DOESN'T WORK
$movies = Movies::find(["columns" => "id, title", "limit" => "10"]);
foreach ($movies as $movie){
    var_dump($movies->genres); // DOESN'T WORK :C
}

What can i do?, i don't need all columns for example in this case i just need the title, but also need the genres.

Thanks.



11.6k

I think you should do like this, no? :

        $movies = Movies::find(array(
                "columns" => array("id, title"),
                "limit" => "10")
                );

or using [...] notation, but encapsulating your column names too

edited Aug '16

You can't do it because you don't have full object returned. Using columns will return Phalcon\Mvc\Model\Row, it doesn't know anything abotu your model not your model. Just return full models if you want to use relatins or use query builder. I prefer this second option.

Yeah, the problem is that i don't need all the columns, in this moment im using raw SQL, but i want to know if i could do it with models without bring all columns.