2012-12-16 94 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