I do a sample of data from the database. You must select all the products in the current category, as well as in its subcategories. It is easy to do this without a dataprovider if there are connections in the models. But I really want to use dataprovider.

In Yii2, the following code is used to fetch data:

$searchModel = new FooSearch; $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams()); 

However, the parameters passed by GET need to add default parameters (in my case - category id). If the parameter is one, then it can be done like this :

 $searchModel = new FooSearch; $dataProvider = $searchModel->search(ArrayHelper::merge( Yii::$app->request->queryParams, [$searchModel->formName() => ['id_bar' => $id]] )); 

Or so .

But how to transfer not one parameter, but an array ?

    1 answer 1

    You can assemble a list of subcategories into a string using implode and pass it to SearchModel as indicated above in the question.

    And actually in the corresponding SearchModel you need to make a couple of changes:

    1. To transfer the category id from integer to safe in the rules () method ( something is now transmitted not a number, but a string).

    2. Put the filter in the search () method in a separate block. It was:

       $query->andFilterWhere([ 'id_category' => $this->id_category, ]); 

      It became:

       $query->andFilterWhere(['in', 'id_category', explode(',', $this->id_category)]);