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

Model relations

hello. please tell me is there any way to use Model relations to build same query "SELECT c.* FROM Category AS c LEFT JOIN Video AS v ON (v.cat_id=c.id) WHERE c.enable = 1 GROUP BY v.cat_id HAVING COUNT(v.id)>0";

category: public function initialize() { $this->hasMany('id', 'Video', 'cat_id'); } video: public function initialize() { $this->belongsTo("cat_id", "Category", "id"); }



98.9k
Accepted
answer

Conditions are automatically added to the query if they're omitted in the join conditions clause:

$phql = "SELECT c.* FROM Category AS c LEFT JOIN Video AS v WHERE c.enable = 1 GROUP BY v.cat_id HAVING COUNT(v.id)>0";