开发者

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('option开发者_Python百科s',
    $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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜