cakephp OR条件 [英] cakephp OR condition

查看:56
本文介绍了cakephp OR条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初是在cakephp问答上发布的发布,但我将其放在此处以希望得到一些答案

Originaly posted on cakephp Q&A but i'll put it up here in hope of getting some answers.

我有一堆公司的默认状态为0,但有时状态更高。现在,我想使用高状态(如果存在),但如果不存在则恢复为0。我尝试了很多不同的方法,但是我总是只能得到状态为0或状态为我想要的方法,从不给我状态(如果存在)和0(如果不存在)。

I have a bunch of companies that has a status of 0 as default but sometimes get a higher status. Now i want to use the high status if exists but revert to 0 if not. i have tried a bunch of different approaches but i always get either only the ones with status 0 or the ones with the status i want, never giving me status if exists and 0 if not.

只给我我指定的状态,而不给我状态0:

Gives me only the status i specify, not giving me the ones with status 0:

'Company' => array (
    'conditions' =>  array (
        'OR' => array(
            'Company.status' => 0,
            'Company.status' => $status,
        )

    )
)

只给我0状态:

'Company' => array (
    'conditions' =>  array (
        'OR' => array(
            'Company.status' => $status,
            'Company.status' => 0
        )
    )
)

状态定义并在代码中检索数据:

Status definition and retrieving data in code:

function getCountry($id = null, $status = null) {
    // Bunch of code for retrieving country with $id and all it's companies etc, all with status 0.
    $status_less_companies = $this->Country->find...

    if ($status) {
        $status_companies = $this->Country->find('first', array(
            'conditions' => array(
                'Country.id' => $id
            ),
            'contain' => array(
                'Product' => array (
                    'Company' => array (
                        'conditions' =>  array (
                            'OR' => array(
                                'Company.status' => $status,
                                'Company.status' => 0
                            )
                        )
                    )
                )
            )
        )
    }

    // Mergin $status_less_companies and $status_companies and returning data to flex application.
}

我为这个问题更改了模型的名称,只是为了使其更有意义,当我告诉他们我为我的flex应用程序使用cakephp时,人们通常会吓跑。我想这个问题的逻辑没有道理,但请相信我,这对我的应用程序是有道理的。

I changed the name for the models for this question just to make more sense, people are generaly frighten away when i tell them i work with cakephp for my flex application. I guess the logic to this question doesn't make sense but trust me that it makes sense in my application.

谢谢!

推荐答案

我不确定您希望得到什么结果。如果要检索状态为0的所有记录,再加上状态为3的记录,则可以使用 IN代替 OR。

I'm not sure to have understood what results you expect. If you want to retrieve all records having status = 0, plus let's say the one having status = 3, you could use an 'IN' instead of an 'OR'.

在Cake中,您可以这样写:

In Cake, you would write it like this:

$status = 3;
$conditions = array('Company.status' => array(0, $status));

这篇关于cakephp OR条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆