We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.8k
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";