Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp OR condition

Tags:

php

cakephp

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!

like image 866
eldamar Avatar asked Dec 13 '11 07:12

eldamar


People also ask

What is CakePHP used for?

CakePHP is a PHP, object-oriented, Model-View-Controller framework, designed around providing the tooling to let you rapidly build web applications. CakePHP focuses on solving problems rapidly, by using conventions over configuration, to enable you to work sooner, without making a lot of decisions upfront.

Which is better CakePHP or laravel?

Laravel has a better approach and thus is more preferable to CakePHP in terms of data backup and handling perspective. CakePHP mainly works on smaller projects and thus is less preferable in terms of data backup and handling perspective.

Which is better CakePHP or CodeIgniter?

If you prefer the MVC pattern then you'd naturally favor the CakePHP over CodeIgniter. However, if you are new to frameworks, you might prefer CodeIgniter since it is a lot simpler and easier to learn.

Is CakePHP a MVC?

CakePHP follows the MVC software design pattern. Programming using MVC separates your application into three main parts: The Model represents the application data. The View renders a presentation of model data.


2 Answers

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

like image 106
Vigrond Avatar answered Oct 13 '22 05:10

Vigrond


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));
like image 43
nIcO Avatar answered Oct 13 '22 06:10

nIcO