19

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!

eldamar
  • 659
  • 1
  • 7
  • 19

4 Answers4

58

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

Vigrond
  • 8,148
  • 4
  • 28
  • 46
  • That gives me the same result as my second example, only display every company with status = 0 and not using the defined status if they exists. – eldamar Dec 13 '11 at 08:16
  • Can you show us how $status is declared, and an example of what values are in the database? – Vigrond Dec 13 '11 at 08:24
  • Updated my question with some additional information, does this help? – eldamar Dec 13 '11 at 08:38
  • Hard to say, I'd recommend getting the raw SQL that it is outputting to help you debug it further. You can get that through DataSource::_queriesLog, here are some instructions http://stackoverflow.com/questions/2521225/cakephp-get-last-query-run – Vigrond Dec 13 '11 at 09:02
  • Having a bit of trouble using the function getLastQuery() from that response, doesn't seem to give me the latest query from the model i want :S working on this. – eldamar Dec 13 '11 at 12:19
  • Guess i figured it out a bit, OR gives me both result when changing parent Model to hasMany, i guess what i was looking for was some kind of IF statement instead of OR – eldamar Dec 13 '11 at 13:16
  • It was wrapping the `OR` conditions in separate arrays that did it for me. – Simon Oct 01 '14 at 21:37
29

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));
nIcO
  • 5,001
  • 24
  • 36
  • This answer beats Vigronds. Although both methods work, using IN is much more efficient as using OR and thus the entire application becomes faster. – pudelhund Jul 18 '13 at 10:18
  • In OPs example this one may be better, but in the case of an `OR` spanning more than one field, @Vigrond's answer works for both cases. – dKen Oct 23 '14 at 16:45
  • I wish I could give you more than a thumbs up. Was stuck on this for too long. – Millar248 Nov 01 '19 at 17:02
  • *Cannot convert value of type `array` to integer*. You may have to use the `IN` keyword explicitly: https://stackoverflow.com/questions/26887511/how-to-create-a-in-clause-in-cakephp-query/26887717#26887717 – ᴍᴇʜᴏᴠ Jan 06 '22 at 05:05
0

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

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

I hope you will find answer.

Rashid Khan
  • 328
  • 3
  • 12
-2
    $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,
                ]
            ],
        ]
    ]);
  • 3
    Please read [answer] and [edit] your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Adriaan Sep 01 '22 at 08:18