Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: conditions with hasMany

Tags:

cakephp

I have a Blog model that has defined var $hasMany=array("Comment");

How can i apply a filter on the Comments to select only the Blog items that match a certain Comment category?

If, in the Blog model, i do

$this->recursive=2;
$this->hasMany=array("Comment");  //in Comment i have $belongsTo("Blog")
return $this->find("all",array("conditions"=>array("Comment.comment_type_id"=>123)));

i get an error that says Unknown Comment.comment_type_id column because Cake does not make the join.

I think this is a rather common issue so i believe simple to solve but i can't find a good way

like image 286
Mike Avatar asked Sep 16 '26 16:09

Mike


1 Answers

 $this->bindModel(array(
    'hasMany' => array(
        'Comment' => array(
            'conditions' => array('Comment.comment_type_id' => 123)
         ))));
$this->find('all');

or if the Model is already binded with comment use :

$this->hasMany['Comment']['conditions'] = array('Comment.comment_type_id' => 123);
$this->find('all');
like image 97
Ibrahim Avatar answered Sep 19 '26 11:09

Ibrahim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!