简体   繁体   中英

cakephp OR condition

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

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.

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

    )
)

Gives me only status of 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.
}

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.

Thanks!

Try

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

    )
)

In the cookbook it says to wrap the or conditions in arrays if they are pertaining to the same field http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

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

In Cake, you would write it like this:

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

You can also fetch record by using following method: put values in an array eg $arr=array(1,2);

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

I hope you will find answer.

    $this->loadModel('Color');
    $colors = $this->Color->find('all', [
        'conditions' => [
            'Color.is_blocked' => 0,
            'Color.is_deleted' => 0,
            'OR' => [
                [
                    'Color.isAdmin' => $user_data['id'],
                ],
                [
                    'Color.isAdmin' => 0,
                ]
            ],
        ]
    ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM