CakePHP多个嵌套查询

时间:2012-10-26 08:39:11

标签: mysql cakephp-1.3

我希望我的查询为:

$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的条件,知道为什么?

请指导我!

2 个答案:

答案 0 :(得分: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')
            )
               )

答案 1 :(得分: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'))
);