cakephp 或条件

新手上路,请多包涵

最初 发布 在 cakephp Q&A 上,但我会把它放在这里,希望能得到一些答案。

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

只给我我指定的状态,而不给我状态为 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
        )
    )
)

代码中的状态定义和检索数据:

 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 时,人们通常会害怕。我想这个问题的逻辑没有意义,但相信我,它在我的应用程序中是有意义的。

谢谢!

原文由 eldamar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 404
2 个回答

我不确定是否了解您期望的结果。如果要检索状态 = 0 的所有记录,再加上状态 = 3 的记录,则可以使用“IN”而不是“OR”。

在 Cake 中,你可以这样写:

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

原文由 nIcO 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏