Yii2 Active record query multiple where, where is a sentence from an array

The default data provider for the Yii2 index page is as follows:

$dataProvider = new ActiveDataProvider([ 'query' => ModelName::find(), ]); 

Now I have an array like $arr = [1, 2, 4, 6];

I want to add a where clause as:

 WHERE parentId=1 OR parentId=2 OR parentId=4 OR parentId=6 

How can i do this?

+7
yii2
source share
1 answer

You can do this:

 $query = ModelName::find()->where(['parentId' => $arr]); $dataProvider = new ActiveDataProvider([ 'query' => $query, ]); 

When you pass an array to where , Yii automatically converts it to an IN condition.

Thus, the formed conditional part of SQL will be WHERE parentId IN (1, 2, 4, 6); .

This is equivalent to the specified condition with OR .

+11
source share

All Articles