azlar azlar - 1 year ago 346
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
condition ?

Answer Source

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'],

Read more about ActiveQuery::where().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download