2011-06-17 73 views
0

我想將下面的mysql語句轉換成cakephp查詢語法。請幫助我。這是我的查詢。 SELECT * FROM post WHERE (user_id == $id OR awarded_to = $id) AND id = $idcakephp find()問題

這是我的cakephp查詢。這是錯的嗎。

$this->Post->find('all', 
      array('conditions' => array('OR' => 
             array('Post.user_id' => $id), 
             array('Post.awarded_to' => $id) 
             ), 
             'AND' => 
             array('Post.id' => $id) 
       )) 

回答

4

這是一個人的房子,但下次read the docs

$this->Post->find('all', array(
      'conditions' => array(
       'OR' => array(
        array('Post.user_id' => $id), 
        array('Post.awarded_to' => $id) 
       ), 
       'Post.id' => $id 
      ) 
     )); 

的2個OR陣列需要是一個數組自理的內部,而且也沒有必要AND

0

只需添加Post.id = $ ID下面的OR條件,而不給你「和」它是自動讀取爲和條件......

$this->Post->find('all', array(
     'conditions' => array(
      'OR' => array('Post.user_id' => $id), 
       array('Post.awarded_to' => $id) 
     ), 
     'Post.id' => $id 
    )); 
0

你非常接近與您的查詢,只需要點點修改喜歡後除去 'AND' 條件在數組 'OR' 爲:

$this->Post->find('all', array(
     'conditions' => array(
      'OR' => array(
       array('Post.user_id' => $id), 
       array('Post.awarded_to' => $id) 
      ), 
      'Post.id' => $id 
     ) 
    )); 

或者

$this->Post->find('all', array(
     'conditions' => array(
      'Post.id' => $id 
     ) 
    )); 

的如果您在AND條件中傳遞表主鍵'id',第二個選項將會是最適合您的選項。