Set the where clause for a dataparauder in a specific controller method

I want to set the condition for only one action in the controller, so I do not want to change my search model.

My code is as follows:

 public function actionRoles() { $searchModel = new EmployeeSearch(); //$searchModel->query()->where('role <> regular'); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('view_role', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } 

The line $searchModel->query()->where('role <> regular'); shows my condition ( $searchModel->query()->where('role <> regular'); ), this is pretty simple, but I did not find a solution that works on the Internet.

For reference, I tried:

+7
search condition yii2 model dataprovider
source share
6 answers

Ok, I did it, it works for me this way:

 public function actionRoles() { $searchModel = new EmployeeSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $dataProvider->sort = ['defaultOrder' => ['role'=>SORT_ASC, 'fullname'=>SORT_ASC]]; $dataProvider->query->where('employee.role <> \'regular\''); return $this->render('view_role', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } 

Of course, it’s a little more complicated and it will probably be better to do this in the model, but I want him to use it in this action and have a bunch of other actions with the same search model, but with different conditions.

+10
source share

This can be done in the controller.

 $searchModel = new ModelSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $dataProvider->query->andWhere(['lang'=>'ENG']); 
+7
source share

You can try this way

 $searchModel = new EmployeeSearch(); $searchModel->role = 'regular'; $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 

In the search model:

 $query->andFilterWhere(['<>', 'role', $this->role]); 

The second way to pass the second parameter, for example:

 $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $role = 'regular'); 

In the search model

 if($role == 'regular') { $query->andWhere(['<>', 'role', $this->role]); } 

Another way to pass another parameter, for example, a problem during filtering:

 $dataProvider = $searchModel->search(Yii::$app->request->queryParams+['EmployeeSearch' => ['<>', 'role' =>'regular']]); 
+2
source share

You can try the following:
SearchModel:

 $searchModel = new EmployeeSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $query->andFilterWhere(['<>', 'role'=>'regular']); return $this->render('view_role', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); 

Also follow this link: http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html

0
source share

Try this solution.

 $searchModel = new ModelnameSearch ( [ 'Attribute' => 1, 'SecondAttribte' => '1', ] ); 
0
source share

Try this with multi-params -

 $searchModel = new YourSearchModel(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $dataProvider->query->where('field1 = :field1 AND field2 = :field2', [':field1' => 1, ':field2' => 'A']); 
0
source share

All Articles