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

2019-01-15 00:36发布

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.

4条回答
Deceive 欺骗
2楼-- · 2019-01-15 01:09

Try this one for CakePHP 3.x

$query = $restaurants
    ->find()
    ->where(['id IN' => $r_ids]);
$query->all();
查看更多
Ridiculous、
3楼-- · 2019-01-15 01:12

You can also use the short syntax:

    $result = $restaurants->find('all', 
            ['conditions' => ['id IN' =>$r_ids]]
        )->all();
查看更多
SAY GOODBYE
4楼-- · 2019-01-15 01:23

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

查看更多
疯言疯语
5楼-- · 2019-01-15 01:23

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[]'
        ])
        ;
查看更多
登录 后发表回答