【问题标题】:How to create a `IN` clause in CakePHP query?如何在 CakePHP 查询中创建“IN”子句?
【发布时间】:2015-01-09 08:05:14
【问题描述】:

如何在新的 CakePHP 中设置 where in 子句?我正在尝试:

$restaurants->find()->where(['id' => $r_ids])->all();

$r_ids 是我在查询中需要的 id 数组,但这不能按预期工作。

【问题讨论】:

    标签: cakephp cakephp-3.0 query-builder


    【解决方案1】:

    在 Cakephp 3.x 中,如果您有多个 where 条件,那么您的查询将如下所示:

    return $this
            ->find()
            ->contain([
                'XYZ.Articles',
                'YYY.Managers',
                'ZZZ.Readers',
            ])
            ->where([
                'ID' => $ids,
                'READER.STATUS' => $xyz,
            ],[
                'ID' => 'integer[]'
            ])
            ;
    

    【讨论】:

      【解决方案2】:

      你也可以使用短语法:

          $result = $restaurants->find('all', 
                  ['conditions' => ['id IN' =>$r_ids]]
              )->all();
      

      【讨论】:

        【解决方案3】:

        在 CakePHP 3.x 上试试这个

        $query = $restaurants
            ->find()
            ->where(['id IN' => $r_ids]);
        $query->all();
        

        【讨论】:

          【解决方案4】:

          在 CakePHP 3.x 中,现在需要指明数据类型,对于值数组,需要将 [] 附加到类型:

          $query = $articles
              ->find()
              ->where(['id' => $ids], ['id' => 'integer[]']);
          

          或明确使用IN 关键字:

          $query = $articles
              ->find()
              ->where(['id IN' => $ids]);
          

          另见

          【讨论】:

          • 如果我们必须检查 IN 子句中的多个列,那该怎么办?在这种情况下该怎么做:->where(['id,name IN' => ??]);
          • @rack_nilesh 你必须使用元组比较表达式:stackoverflow.com/a/37924543/1392379
          • tysm。这就是我一直在寻找的。​​span>
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-02-25
          • 1970-01-01
          • 2012-04-24
          • 2020-10-13
          • 1970-01-01
          • 2015-04-02
          • 2010-10-23
          相关资源
          最近更新 更多