2012-12-16 43 views
-1

我有以下查询应寻找:CakePHP的查询与和或

  • 其中USER_ID匹配的帐户及friend_id比赛friendId
  • 或行,其中USER_ID匹配friendId和friend_id匹配用户id一行
  • 和1个

我的代码都也有状态为:

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

然而,它不工作......我环顾四周,似乎是在处理两个AND调用时正确地获取数组,但我不明白。谁能帮我吗?

感谢

+2

你应该知道,你不能使用数组键两次在同一个阵列中。 – mark

+0

我不知道。因此,这个问题。我在这里寻找帮助http://stackoverflow.com/questions/5649185/cakephp-find-all-condition-and-or这就是我如何把我的代码放在一起。该页面上的 – Cameron

+0

这正是我的解决方案,您使用了Jason所称的代码,例如“而不是”。你必须使用下面的代码块 – flo

回答

2

正如马克说你不能使用相同的数组键两次,所以你必须封装与运算在单独的阵列

'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 
) 
+0

谢谢。阵列太多,它让我感到困惑;) – Cameron