cakephp找到所有条件和OR

时间:2011-04-13 12:27:43

标签: cakephp find nested conditional-statements

以下是我的情况:

'OR' =>
    array(
        'AND' => array(
            array('EventCompetitor.is_black' => 1),
            array('EventCompetitor.is_adult' => 1)
        ),
        'AND' => array(
            array('EventCompetitor.is_black' => 0),
            array('EventCompetitor.is_adult' => 0)
        ),
    ),

当我调试我的查询时,它就是这样的,这是错误的:

AND ((`EventCompetitor`.`is_black` = 0) AND (`EventCompetitor`.`is_adult` = 0)) AND

现在,这不是我想要的,我希望它是这样的:

((`EventCompetitor`.`is_black` = 1) AND (`EventCompetitor`.`is_adult` = 1)) OR ((`EventCompetitor`.`is_black` = 0) AND (`EventCompetitor`.`is_adult` = 0))

任何想法,我怎样才能实现它?

谢谢!

3 个答案:

答案 0 :(得分:16)

“OR”键控数组中的子键名称相同,第二个键有效地覆盖第一个键。你需要将这些“AND”条件封装在它们自己的数组中,以防止密钥冲突。

例如。而不是:

'OR' =>
        array(
               'AND' => array(
                              array('EventCompetitor.is_black' => 1),
                              array('EventCompetitor.is_adult' => 1)
                        ),
               'AND' => array(
                              array('EventCompetitor.is_black' => 0),
                              array('EventCompetitor.is_adult' => 0)
                        ),
             ),

做的:

'OR' =>
        array(
               array('AND' => array(
                              array('EventCompetitor.is_black' => 1),
                              array('EventCompetitor.is_adult' => 1)
                        )),
               array('AND' => array(
                              array('EventCompetitor.is_black' => 0),
                              array('EventCompetitor.is_adult' => 0)
                        )),
             ),

答案 1 :(得分:3)

我没有测试过,但我认为您需要以下内容:

'OR' =>
    array(
        array(
            array('EventCompetitor.is_black' => 1),
            array('EventCompetitor.is_adult' => 1)
        ),
        array(
            array('EventCompetitor.is_black' => 0),
            array('EventCompetitor.is_adult' => 0)
        )
    )
条件之间隐含

AND,您只需要定义OR

答案 2 :(得分:0)

我认为你需要一个额外的数组(),我不确定,但值得一试:

'OR' =>
    array(
        array(
            'AND' => array(
                array('EventCompetitor.is_black' => 1),
                array('EventCompetitor.is_adult' => 1)
            )
        ),
        array(
            'AND' => array(
                array('EventCompetitor.is_black' => 0),
                array('EventCompetitor.is_adult' => 0)
            )
        )
    ),