简体   繁体   English

使用AND OR的CakePHP查询

[英]CakePHP query with AND OR

I have the following query which should be finding: 我有以下查询应该找到:

  • A row where the user_id matches userId and friend_id matches friendId user_id与userId匹配且friend_id与friendId匹配的行
  • OR a row where the user_id matches friendId and friend_id matches userId 或一行,其中user_id与friendId匹配,并且friend_id与userId匹配
  • AND both also have status of 1 AND的状态均为1

My code is: 我的代码是:

public function isFriend($userId, $friendId)
{
    return $search = $this->Friend->find('first', array(
            'conditions'=>array(
                'OR' => array(
                    'AND'=>array(
                        'Friend.user_id'=>$userId,
                        'Friend.friend_id'=>$friendId
                    ),
                    'AND'=>array(
                        'Friend.user_id'=>$friendId,
                        'Friend.friend_id'=>$userId
                    )
                ),
                'AND' => array(
                    'Friend.status'=>1
                )
            )
        )
    );
}

However it's not working... I've looked around and seems it's to do with getting the arrays correctly when dealing with the two AND calls, but I don't get it. 但是它不起作用...我环顾四周,似乎与处理两个AND调用时正确获取数组有关,但我不明白。 Can anyone help me out? 谁能帮我吗?

Thanks 谢谢

You can't use the same array key twice as mark said, so you have to encapsulate the ANDs in separate arrays 您不能像mark一样使用两次相同的数组键,因此必须将AND封装在单独的数组中

'OR' => 
array(
    array('AND'=>array(
            'Friend.user_id'=>$userId,
            'Friend.friend_id'=>$friendId
        )
    ),
    array('AND'=>array(
            'Friend.user_id'=>$friendId,
            'Friend.friend_id'=>$userId
        )
    )
),
'AND' => array(
    'Friend.status'=>1
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM