How to create a `IN` clause in CakePHP query?

好久不见. 提交于 2019-11-26 21:23:00

问题


How do you make it where in clause in new CakePHP? I'm trying:

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

Where $r_ids is array of ids I need in my query, but this doesn't work as expected.


回答1:


With CakePHP 3.x it's now necessary to either indicate the data type, which for an array of values need to have [] appended to the type:

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

or to explicitly make use of the IN keyword:

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

See also

  • Cookbook > Database Access & ORM > Query Builder > Automatically Creating IN Clauses



回答2:


Try this one for CakePHP 3.x

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



回答3:


You can also use the short syntax:

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



回答4:


In Cakephp 3.x, If you have multiple where conditions then your query will be as below:

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


来源:https://stackoverflow.com/questions/26887511/how-to-create-a-in-clause-in-cakephp-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!