开发者

cakephp find all condition AND OR

Below is my condition:

'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)
        ),
    ),

When I debug my query, it comes something like this, which is wrong:

AND ((`EventCompetitor`.`is_black` = 0) AND 开发者_JS百科(`EventCompetitor`.`is_adult` = 0)) AND

Now, that's not what I want, I want it something like this:

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

Any idea, how can I achieve it?

Thanks !


your subkeys in your "OR" keyed array are the same name, with the second key effectively overwriting the first. you need to encapsulate those "AND" conditions in their own arrays, to prevent the key collision.

eg. instead of:

'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)
                        ),
             ),

do:

'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)
                        )),
             ),


I haven't tested it, but I think you need the following:

'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 is implied between conditions, you only have to define OR groups


I think you need an extra array() in there, im not sure but its worth a try:

'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)
            )
        )
    ),
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜