Set a where condition for dataprovider in specific controller method
Asked Answered
S

6

12

I am looking to set a condition only for a single action in the controller, so I don't want to change my search model.

My code looks like this:

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 commmented row shows my condition ($searchModel->query()->where('role <> regular');), it's pretty straightforward but I have not found a solution that works online.

For reference I tried those:

Steric answered 31/7, 2015 at 8:40 Comment(0)
S
15

Ok, I got it done, it works this way for me:

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,
    ]);
}

Certainly a bit complicated and doing it in the model would probably be better, but I only want it to use it in this action and have a bunch of other actions with the same searchmodel but different conditions.

Steric answered 31/7, 2015 at 12:6 Comment(0)
S
11

You can do it this way in the Controller

$searchModel = new ModelSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andWhere(['lang'=>'ENG']);
Stegall answered 17/8, 2016 at 8:11 Comment(0)
N
4

You can try this way

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

In Search model :

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

Second way pass second parameter like :

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

In search model

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

Another way pass other parameter like but problem in filter time:

$dataProvider = $searchModel->search(Yii::$app->request->queryParams+['EmployeeSearch' => ['<>', 'role' =>'regular']]);
Negativism answered 31/7, 2015 at 9:12 Comment(2)
I only so far (have to go for lunch, will try afterwards the rest) tested your last one, that returns every entry with role regular. Will report back on the rest.Steric
I don't want to modify the model because I am using it at many places and this is just one view, so any changes to the model are annoying.Steric
M
0

You can try this:
SearchModel:

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


    $query->andFilterWhere(['<>', 'role'=>'regular']);
return $this->render('view_role', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);

Please also visit this link:http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html

Multiplier answered 31/7, 2015 at 9:41 Comment(3)
This returns: PHP Notice – yii\base\ErrorException Undefined variable: querySteric
also include 'query'=>$query in return sectionMultiplier
It doesn't work because $query is not a defined model.Steric
R
0

Try this solution

$searchModel = new ModelnameSearch
           (
              [ 'Attribute' => 1,
               'SecondAttribte' => '1',
              ]
           );
Recondite answered 5/5, 2016 at 6:27 Comment(0)
G
0

Try like this using multi-params -

$searchModel = new YourSearchModel();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->where('field1 = :field1 AND field2 = :field2', [':field1' => 1, ':field2' => 'A']);
Glomma answered 15/5, 2017 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.