Piotr Chabros
Piotr Chabros

Reputation: 475

Conditions in cakePHP

I am trying to find countries with conditions set as a variable, it looks like this:

$conditions = '1,2,3';
$this->set('options',
    $this->Agency->Country->find('list',
        array(
            'conditions' => array(
                'Country.zone_id' => array($conditions)
             ) 
        )
    )
);

This should result in finding all the countries with zone_id = 1 or 2 or 3.

But in this case only the first value in the $conditions is taken to an account, so in this case it works like Country.zone_id' => array(1). It returns only the countries with zone_id = 1. Why all the other are skipped?

Upvotes: 1

Views: 996

Answers (1)

vstm
vstm

Reputation: 12537

I think the value is converted to an int which means the rest after "1" is ignored. If an array is supplied then it behaves like an IN clause in SQL with each array entry as a value to be checked against.

So if you have to use a string with each ID separated by a comma then try the following:

'conditions' => array(
    'Country.zone_id' => explode(",", $conditions)
)

Otherwise if you have already an array of numerical ID's then you can assign it directly:

'conditions' => array(
    'Country.zone_id' => array(1, 2, 3),
)     

Upvotes: 1

Related Questions