Using CRUD with generated GridView on the index page.

At the moment in the GridView displays all the elements from the table in the database.

I wanted that all data would not be displayed, but only those for which, for example, the field some_id in the table is 12 .

I tried this:

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

Posted

 $query = SomeModel::find()->where(['some_id' => 12]); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 20, ], ]); 

It seems everything turned out, but now searchModel does not work.

Or you can add a condition to the string

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

UPDATE:

In fact, in the controller, actionIndex accepts the parameter some_id , and then displays the data for which the condition is met.

 public function actionIndex($some_id){ $searchModel = new SomeModelSearch(); // Я сам закомментировал //$dataProvider = $searchModel->search(Yii::$app->request->queryParams); // И добал это $query = SomeModel::find()->where(['some_id' => $some_id]); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 20, ], ]); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } 
  • Put the full code. Here's how: $ dataProvider-> query-> andWhere (['some_id' => '12']); - Urmuz Tagizade
  • searchModel does not work for you for another reason, I just checked it, this option works fine, so I wrote @UrmuzTagizade, I need more code - MasterAlex
  • now lay out the code - Xfirab
  • @MasterAlex or my question is poorly formulated, or I do not understand well. Therefore, in simple terms, I wanted to get only those data. conditions dynamically. Maybe it's done differently - Xfirab
  • @MasterAlex Hello, help, please, on my question :) Link - Dima

1 answer 1

In the $searchModel in the search method at the end, before return $dataProvider add your query, for example, like this:

 $query->andFilterWhere(['some_id' => 12]); 
  • In principle, it came up. Only I added $_GET['some_id'] instead of 12 and everything works. It just seems to me that there is a more beautiful solution than my быдло_код - Xfirab
  • one
    @Xfirab is a more beautiful solution, in the controller you make an array of $ inputParams in which you stuff arguments, then you combine it with queryParams through array_merge and bustle it all into the search model set up the necessary validation rules for all action arguments and queryParams passed to SerachModel and there inside the search method hook andFilterWhere to $ query. And don't use raw data from $ _GET - fens
  • @Yaroslav Molchan Hello, help, please, on my question :) Link - Dima