条件使用型号 - &GT关联模型;发现()(CakePHP的) [英] Conditions in associated models using Model->find() (CakePHP)

查看:115
本文介绍了条件使用型号 - &GT关联模型;发现()(CakePHP的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有CakePHP的find()方法,并在更深模型关联条件的一些问题。有一些这些周围,但我无法找到一个答案为止。

我的模型协会是用户的hasMany帖子的hasMany 投票的belongsTo评论的belongsTo后的belongsTo用户分别为意见的hasMany投票。在的belongsTo 协会使用内部连接(类型=>内在)。

我如何找到所有评论投票与CakePHP的模型 - >查找特定用户的帖子()方法?

我用了四个型号故意链,因为这似乎对直接关联模型的条件下工作。所以使用在邻近表的外键,保持柱(条件'Post.user_id == 1'而不是'User.id == 1)没有。

在SQL这将是:

SELECT诉*
FROM票v
    JOIN评论C ON(v.comment_id = c.id)
    JOIN帖子p在(c.post_id = p.id)
    用户加入ûON(p.user_id = u.id)
WHERE u.id = 1

我无法重现这些连接使用find()+中容纳的行为。虽然我可以简单地让他的所有数据的用户,我将不得不从产生的数组中收集所有选票。

这不工作像这样(警告:未知列'User.id')

  $这个 - > Vote->递归= 2; // 或更高
$这个 - > Vote->找到('所有',阵列('条件'=>阵列('User.id'=> 1)));

事实上,这甚至不使用POST而不是用户(Vote-> Comment->邮政),只要我添加条件下工作。所生产的SQL查询只联接投票和评论。

在返回数组应该只包含上面的票数在SQL查询将返回,其他一切都应该是在这个过程中走加盟。

请注意:我的问题是相当接近这一个,这让我入门:
<一href=\"http://stackoverflow.com/questions/813294/in-cakephp-how-can-i-do-a-find-with-conditions-on-a-realted-field\">In CakePHP的我怎么能做到与相关领域的条件找?


解决方案

  $ =加入阵列(
           阵列('表'=&GT;意见,
                 别名'=&GT; '评论',
                 '型'= GT;'内',
                 '条件'=&GT;阵列(
                 Comment.id = Vote.comment_id
           ))
           阵列('表'=&GT;'帖子',
                 别名'=&GT; 邮报,
                 '型'= GT;'内',
                 '条件'=&GT;阵列(
                 Post.id = Comment.post_id
           ))
           阵列('表'=&GT;'用户',
                 别名'=&GT; '用户',
                 '型'= GT;'内',
                 '条件'=&GT;阵列(
                 User.id = Post.user_id','User.id'=&GT; $ USER_ID
           ))
         );$票= $这个 - &GT; Vote-&GT;找到('所有',阵列('加入'=&GT; $连接,'递归'=&GT; -1));

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 so far.

My model associations are User hasMany Post hasMany Comment hasMany Vote and Vote belongsTo Comment belongsTo Post belongsTo User respectively. The belongsTo associations use inner joins ('type' => 'INNER').

How do I find all comment votes for posts of a specific user with CakePHP's model->find() method?

I used a chain of four models deliberately, because this seems to work for conditions in directly associated models. So there is no using the foreign-key-holding column in the neighbouring table (condition 'Post.user_id == 1' instead of 'User.id == 1').

In SQL this would be:

SELECT v.* 
FROM votes v 
    JOIN comments c ON (v.comment_id = c.id)
    JOIN posts p ON (c.post_id = p.id)
    JOIN users u ON (p.user_id = u.id)
WHERE u.id = 1

I am unable to reproduce these joins using find() + the Containable behavior. Although I could simply get a user with all his data, I would then have to collect all votes from inside the resulting array.

It is not working like this (Warning: unknown column 'User.id'):

$this->Vote->recursive = 2; // or higher
$this->Vote->find('all',array('conditions' => array('User.id' => 1)));

In fact, this doesn't even work using Post instead of User (Vote->Comment->Post) as soon as I add the condition. The manufactured SQL query only joins votes and comments.

The returning array should only contain votes the SQL query above would return, everything else should be "joined away" in the process.

Note: My question is quite close to this one, which helped me getting started: In cakephp how can I do a find with conditions on a related field?

解决方案

$joins = array(
           array('table'=>'comments', 
                 'alias' => 'Comment',
                 'type'=>'inner',
                 'conditions'=> array(
                 'Comment.id = Vote.comment_id'
           )),
           array('table'=>'posts', 
                 'alias' => 'Post',
                 'type'=>'inner',
                 'conditions'=> array(
                 'Post.id = Comment.post_id'
           )),
           array('table'=>'users', 
                 'alias' => 'User',
                 'type'=>'inner',
                 'conditions'=> array(
                 'User.id = Post.user_id','User.id'=>$user_id
           ))
         );

$votes = $this->Vote->find('all',array('joins'=>$joins,'recursive'=>-1));

这篇关于条件使用型号 - &GT关联模型;发现()(CakePHP的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆