使用 Model->find() (CakePHP) 的关联模型中的条件 [英] Conditions in associated models using Model->find() (CakePHP)

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

问题描述

我在使用 CakePHP 的 find() 方法和更深"模型关联中的条件时遇到了一些问题.周围有一些,但到目前为止我找不到答案.

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.

我的模型关联分别是 User hasMany Post hasMany Comment hasMany VoteVote ownsTo Comment ownsTo PostbelongsTo User.belongsTo 关联使用内部联接('type' => 'INNER').

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').

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

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

我特意使用了四个模型的链,因为这似乎适用于直接关联模型中的条件.所以没有使用相邻表中的外键保持列(条件 'Post.user_id == 1' 而不是 'User.id == 1').

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').

在 SQL 中,这将是:

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

我无法使用 find() + Containable 行为重现这些连接.虽然我可以简单地获取一个用户的所有数据,但我必须从结果数组中收集所有投票.

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.

它不是这样工作的(警告:未知列User.id"):

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)));

事实上,一旦我添加了条件,这甚至不能使用 Post 而不是 User (Vote->Comment->Post).制造的 SQL 查询仅连接投票和评论.

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.

返回的数组应该只包含上面的 SQL 查询将返回的投票,其他的一切都应该在这个过程中加入".

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

注意:我的问题与这个问题非常接近,这有助于我入门:在 cakephp 中怎么可以我使用相关字段的条件进行查找?

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));

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

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