Conditions in associated models using Model->find() (CakePHP)

后端 未结 4 1796
无人及你
无人及你 2020-12-15 10:56

I am having some issues with CakePHP\'s find() method and conditions in \'deeper\' model associations. There are some of these around but I could not find an answer to this

4条回答
  •  猫巷女王i
    2020-12-15 11:30

    This may be one of those times you need to use the query method.

    SQL calls that you can't or don't want to make via other model methods (careful - there are very few circumstances this is true) can be made using the model's query() method.

    $votes = $this->Vote->query('SELECT Vote.* FROM votes Vote 
        JOIN comments Comment ON (Vote.comment_id = Comment.id)
        JOIN posts Post ON (Comment.post_id = Post.id)
        JOIN users User ON (Post.user_id = User.id)
        WHERE User.id = 1');
    

    This should return an array of Vote entries like the find method would.

    foreach ($votes as $vote):
        echo $vote['Vote']['id'];
    endforeach;
    

提交回复
热议问题