cakephp 查找所有条件 AND OR

发布于 2024-11-01 14:29:35 字数 753 浏览 1 评论 0原文

以下是我的情况:

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

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

谢谢 !

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 (`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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

病女 2024-11-08 14:29:35

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

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)
                        )),
             ),
£噩梦荏苒 2024-11-08 14:29:35

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

'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

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

铜锣湾横着走 2024-11-08 14:29:35

我认为你需要一个额外的 array() 在那里,我不确定,但值得一试:

'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 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)
            )
        )
    ),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文