【问题标题】:CakePHP, selecting MySQL records by one or more IdsCakePHP,通过一个或多个 Id 选择 MySQL 记录
【发布时间】:2013-10-21 20:49:59
【问题描述】:

我想根据从以下位置调用的 ID 列表选择 MySQL 记录:

$ids = $this->params['url']['ids']

其中 ids 是一个或多个 ID,以逗号分隔。我知道我可以使用 explode 来获取数组中的 Id:

$id = explode(",", $ids);

我知道id调用的基本代码是:

$row = $this->Model->find('all',
       array(
              'conditions' => array(
                   'id' => $id['0'],
              ),
              'fields' => array(
                   'id',
                   'name'
              )
       ));

我的问题是如何在不知道我之前拥有的 ID 数量的情况下选择使用多个 ID? IE。 1 个 ID,或 3 个 ID,或 5 个等。

【问题讨论】:

    标签: php mysql sql cakephp


    【解决方案1】:

    这似乎应该适用于'id' => $id。也就是说,传入一个 ID 数组进行搜索以使用 IN 子句。

    【讨论】:

    【解决方案2】:

    将 $id 作为数组传递,CakePHP 自动检测数组是否在条件中给出,它会自动将“=”更改为“IN”mysql。

    $row = $this->Model->find('all',
           array(
                  'conditions' => array(
                       'id' => $id,
                  ),
                  'fields' => array(
                       'id',
                       'name'
                  )
           ));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多