2012-10-26 91 views
0

我想我的查詢爲:CakePHP的多重嵌套查詢

$conditions = array(
         'ManageTrackby.trackby_num NOT LIKE' => '%n/a%', 
         'Balancesheet.subscriber_id' => $_SESSION['Auth']['User']['subscriber_id'], 
         'Balancesheet.show_id' => $this->Session->read('openshowid'), 
         'OR' => array('Balancesheet.season_id' => $season_id, 'Balancesheet.season_id is null'), 
         'OR' => array('Balancesheet.episode_id'=> $episode_id, 'Balancesheet.episode_id is null') 
        ); 

其中這裏跳過最後OR選項,並得到下述結果:

Array 
(
[ManageTrackby.trackby_num NOT LIKE] => %n/a% 
[Balancesheet.subscriber_id] => 105 
[Balancesheet.show_id] => 56 
[OR] => Array 
    (
     [Balancesheet.episode_id] => 86 
     [0] => Balancesheet.episode_id is null 
    ) 

這裏,從錯過SEASON_ID條件,任何想法爲什麼?

請指導我!

+0

這是你想要的查詢嗎? 'Balancesheet.season_id = $ season_id OR Balancesheet.season_id = Null AND Balancesheet.episode_id = $ episode_id OR Balancesheet.episode_id = Null' – Bob

回答

0

試試這個。如果你想要所有的第一個條件,加上一個或第二個條件,那麼你會想要在AND數組中的OR。對於複雜的查找,請查看此頁上的信息http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

$conditions = array(
    'AND' => array(
        'ManageTrackby.trackby_num NOT LIKE' => '%n/a%', 
        'Balancesheet.subscriber_id' => $_SESSION['Auth']['User']['subscriber_id'], 
        'Balancesheet.show_id' => $this->Session->read('openshowid') 
      ) 
    'OR' => array(
    array('Balancesheet.season_id' => $season_id, 'Balancesheet.season_id is null'), 
      array('Balancesheet.episode_id'=> $episode_id, 'Balancesheet.episode_id is null') 
      ) 
       ) 
0

您的數組正在設置OR元素兩次。把兩個OR語句放在他們自己的數組中:

$conditions = array(
    'ManageTrackby.trackby_num NOT LIKE' => '%n/a%', 
    'Balancesheet.subscriber_id' => $_SESSION['Auth']['User']['subscriber_id'], 
    'Balancesheet.show_id' => $this->Session->read('openshowid'), 
array('OR' => array('Balancesheet.season_id' => $season_id, 'Balancesheet.season_id is null')), 
array('OR' => array('Balancesheet.episode_id'=> $episode_id, 'Balancesheet.episode_id is null')) 
);