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!

3
  • Can you attach an SQL Dump? (Use $this->Element('sql_dump'); in your view, making sure Configure::debug > 0).
    – Oldskool
    Dec 13, 2011 at 9:48
  • This wont work since i am calling functions in cake from within my flex application. The queries will not show on my view.
    – eldamar
    Dec 13, 2011 at 12:51
  • Really helpful, Appreciated! Mar 17, 2020 at 7:17

4 Answers 4

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

7
  • 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, 2011 at 8:16
  • Can you show us how $status is declared, and an example of what values are in the database?
    – Vigrond
    Dec 13, 2011 at 8:24
  • Updated my question with some additional information, does this help?
    – eldamar
    Dec 13, 2011 at 8: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 stackoverflow.com/questions/2521225/cakephp-get-last-query-run
    – Vigrond
    Dec 13, 2011 at 9: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, 2011 at 12:19
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));
4
  • 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, 2013 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, 2014 at 16:45
  • I wish I could give you more than a thumbs up. Was stuck on this for too long.
    – Millar248
    Nov 1, 2019 at 17:02
  • Cannot convert value of type array to integer. You may have to use the IN keyword explicitly: stackoverflow.com/questions/26887511/… Jan 6, 2022 at 5: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.

-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,
                ]
            ],
        ]
    ]);
1
  • 3
    Please read How to 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 1, 2022 at 8:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.