cakephp或条件

时间:2011-12-13 07:58:57

标签: php cakephp

在蛋糕问答Q& A上的原始posted但是我会把它放在这里以期得到一些答案。

我有一些公司的默认状态为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.
}

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

谢谢!

3 个答案:

答案 0 :(得分:49)

尝试

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

    )
)

在食谱中,它表示如果它们属于同一个字段,则将数据包装在数组中 http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

答案 1 :(得分:25)

我不确定你理解你期望的结果。如果你想要检索状态为0的所有记录,加上让我们说一个状态为3的记录,你可以使用'IN'而不是'OR'。

在Cake中,你会这样写:

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

答案 2 :(得分:0)

您还可以使用以下方法获取记录: 将值放在数组中 例如$ ARR =阵列(1,2);

 $res = $this->Model->find('all', array(                      
        'conditions' =>array('Model.filedname'=>$arr),
        'model.id' => 'desc'
  ));

我希望你能找到答案。

相关问题