Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 203
onedayiwillchangethis Avatar asked Nov 12 '14 12:11

onedayiwillchangethis


1 Answers

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
like image 101
ndm Avatar answered Nov 02 '22 06:11

ndm