我期待为控制器中的单个操作设置条件,所以我不想更改我的搜索模型.
我的代码如下所示:
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,]); }
通信行显示我的条件($searchModel-> query() – > where(‘role<> regular’);),它非常简单,但我还没有找到一个在线工作的解决方案.
作为参考,我试过这些:
> Yii2 how does search() in SearchModel work?
> Yii2 Modify find() Method in Model search()
> https://github.com/yiisoft/yii2/issues/5668
> criteria Active data provider in Yii 2
好的,我完成了它,它以这种方式为我工作:
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,]); }
当然有点复杂并且在模型中执行可能会更好,但我只希望它在此操作中使用它并且具有相同搜索模型但具有不同条件的一堆其他操作.