php Yii2 SearchModel 中的 search() 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21779085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii2 how does search() in SearchModel work?
提问by Mr Goobri
Please can someone explain how the searchmethod in a Yii2 SearchModelworks? I generated it using Gii. Here it is:
请有人解释一下searchYii2 中的方法是如何SearchModel工作的吗?我使用 Gii 生成它。这里是:
public function search($params){
$query = MyModel::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$this->addCondition($query, 'att1');
$this->addCondition($query, 'att1', true);
$this->addCondition($query, 'att2');
$this->addCondition($query, 'att2', true);
return $dataProvider;
}
This is how I call it:
我是这样称呼它的:
$search = new MyModelSearch();
$myModels = $search->search(['att3' => '3']);
Regardless of what attributes I use in calling search, I always get back the same result - i.e. all the entries in the table. I'm missing something here that I just do not understand.
不管我在调用中使用什么属性search,我总是得到相同的结果——即表中的所有条目。我在这里遗漏了一些我不明白的东西。
Any help would be really appreciated. Thanks.
任何帮助将非常感激。谢谢。
回答by soju
The search()function generated by Giiuse ActiveRecord::load()to set search parameters :
该search()所产生的功能GII使用ActiveRecord::load()设置搜索参数:
load()gets the'FormName'from the model'sformName()method (which you may override), unless the$formNameparameter is given. If the form name is empty,load()populates the model with the whole of$data, instead of$data['FormName'].
load()得到'FormName'从模型的formName()方法(你可以重写),除非$formName指定参数。如果表单名称为空,load()则使用整个$data, 而不是填充模型$data['FormName']。
So you should try :
所以你应该尝试:
$myModels = $search->search(['MyModelSearch'=>['att3'=>3]]);
Or
或者
$myModels = $search->search([$search->formName()=>['att3'=>3]]);
And of course add a condition on att3attribute in search()function :
当然,att3在search()函数中的属性上添加一个条件:
$this->addCondition($query, 'att3');
But if you really want to use $myModels = $search->search(['att3' => '3']);then you should simply replace $this->load($params)with $this->load($params, '').
但如果你真的想使用,$myModels = $search->search(['att3' => '3']);那么你应该简单地替换$this->load($params)为$this->load($params, '').
回答by user3410311
If you want some additional param to pass to search() method, you can change search method like this in SomeSearch.php:
如果你想要一些额外的参数传递给 search() 方法,你可以在 SomeSearch.php 中像这样更改搜索方法:
public function search($params, $additional=0)
{
//...
if($additional==1) {
$query->andWhere(['status'=>['some', 'other']);
}
}
and inside controller:
和内部控制器:
public function actionIndex()
{
$searchModel = new AdminSearch();
$additional=1;
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $additional);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}

