How to use search() function from the model without using Gridview?


When we run CRUD operation then it generate index file also to view all items in GridView layout. We can filter displayed items using individual search input box from the Grid layout.

My question is, how can I use search function from the model to filter using multiple criteria without using Grid layout?


Standard index action contains at the top:

     $searchModel = new MyModelSearch();

     $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

     $dataProvider->sort = ['defaultOrder' => ['my_field_to_sort'=>SORT_ASC]];

That you can change in:

     $searchModel = new MyModelSearch();

     $queryModel = new MyModel();

     $queryModel->attributes =  Yii::$app->request->queryParams;

     // here you can apply your filter

     $queryModel->field_to_filter = 'value_to_be_filtered';

     $dataProvider = $searchModel->search($queryModel->attributes);

     $dataProvider->sort = ['defaultOrder' => ['my_field_to_sort'=>SORT_ASC]];

In this way you use ActiveRecord object, but you can simple fill an array with data from Yii::$app->request->queryParams, change values that you want filter and then pass him to search().