AND / OR条件cakephp查找具有许多关联 [英] AND / OR conditions cakephp FIND for has many association

查看:126
本文介绍了AND / OR条件cakephp查找具有许多关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与模型Comment有很多关联的模型Post。



Post有一个主键post_id,它是Comment的外键。



这两个都有可见的列



我对Post.visible选项有一个有效的查询,我需要添加AND以查找具有Post.visible值之一的所有Post。



对于这些帖子,我需要所有具有Comment.visible值= 1的评论。



我的代码:

  $ conditions = array(
OR => array(
Post.visible => array(
1,
2,
3,
4
),
),
AND =>数组(
Comment.visible => 1

);

$ result = $ this-> Post-> find('all',array(
'order'=>'Post.created DESC',
'条件'=> $条件
));

没有AND的结果是可以的(但是我也得到了visible = 0的注释)。 / p>

当我将条件 Comment.visible => 1放入具有manyassociation时,它很好用(但我不能这样做,因为我需要获取Comment



使用$并显示此错误:



错误:SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'Comment.visible'



当我转储SQL时,注释表为甚至在SELECT子句中也不匹配(在LEFT JOIN中也不匹配)。

解决方案

您可以使用 CakePHP的可容纳行为带有类似这样的东西(这应该起作用,但感觉到可以根据您的需要进行调整):

  //发布模型
public $ recursive = -1;

public $ actsAs = array(‘Containable’);

公共函数getPosts(){
$ posts = $ this-> find('all',
array(
'conditions'=> array(
'Post.visible'=> 1
),
'contain'=> array(
'Comment'=> array(
'conditions' => array('Comment.visible'=> 1)



);
返回$ posts;
}

或者,您可以将关联设置为仅提取注释可见的($code> )(即使这样,我仍然建议像上面一样使用 contain-您只需要指定每次条件):

  //发布模型
public $ hasMany = array(
'Comment '=> array(
'conditions'=> array('Comment.visible'=> 1)

);


I have a model Post that has many association with the model Comment.

Post has a primary key post_id which is Comment s foreign key.

Both of these have a visible column.

I have a working query on Post.visible options, and I need to add the AND to find all Posts that have one of Post.visible values.

For these posts I need all Comments that have a Comment.visible value = 1.

My code:

$conditions = array(
                    "OR" => array(
                        "Post.visible" => array(
                            1,
                            2,
                            3,
                            4
                        ),
                    ),
                    "AND" => array (
                        "Comment.visible" => 1
                    )
                );

$result = $this->Post->find('all', array(
                'order' => 'Post.created DESC',
                'conditions' => $conditions
        ));

The result without the AND is OK (but I get also the Comments with visible = 0).

When I put the condition "Comment.visible" => 1 in the has manyassociation, it works well (but I can not do this, because I need to get the Comment with visibility 0 elsewhere).

With the and it shows this Error:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Comment.visible' in 'where clause'

When I dump the SQL, the comments table is not even matched in the SELECT clause (nor in the LEFT JOIN).

解决方案

You can limit another model's results using CakePHP's Containable Behavior with something like this (this should work, but feel free to tweak per your needs):

//Post model
public $recursive = -1;

public $actsAs = array('Containable');

public function getPosts() {
    $posts = $this->find('all',
        array(
            'conditions' => array(
                'Post.visible' => 1
            ),
            'contain' => array(
                'Comment' => array(
                    'conditions' => array('Comment.visible' => 1)
                )
            )
        )
    );
    return $posts;
}

Or, you can set up your association to only ever pull comments that are visible (even WITH this way, I still recommend using 'contain' like above - you just wouldn't need to specify the condition each time):

//Post model
public $hasMany = array(
    'Comment' => array(
        'conditions' => array('Comment.visible' => 1)
    )
);

这篇关于AND / OR条件cakephp查找具有许多关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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