doujiaoben28596 2011-10-08 12:08
浏览 42
已采纳

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?

  • 写回答

1条回答 默认 最新

  • donglian2106 2011-10-08 12:34
    关注

    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),
    )     
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题