azlar azlar - 3 months ago 13
SQL Question

Yii2 ActiveRecord : composite IN condition

How to write a composite IN condition like

(id, name) in ((1, 'George'), (2, 'Tom'))
when using ActiveRecord's
where()
condition ?

Answer

To create a composite IN condition you can use and array for the column name and value, where the values are indexed by the column name: ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]].

e.g. :

$models = Model::find()->where(['in', ['id', 'name'], [
    ['id' => 1, 'name' => 'George'],
    ['id' => 2, 'name' => 'Tom'],
]])->all();

Read more about ActiveQuery::where().