Yii2's index page's default data provider is like following:
$dataProvider = new ActiveDataProvider([
'query' => ModelName::find(),
]);
Now, I've got an array like $arr = [1, 2, 4, 6];
I want to add a where clause like:
WHERE parentId=1 OR parentId=2 OR parentId=4 OR parentId=6
How can I do that?
Can be done like this:
$query = ModelName::find()->where(['parentId' => $arr]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
When you pass an array to where
, Yii automatically transforms it into IN
condition.
So generated SQL conditional part will be WHERE parentId IN (1, 2, 4, 6);
.
It's equivalent to your mentioned condition with OR
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With