Yii2 how does search() in SearchModel work?
Asked Answered
P

3

28

Please can someone explain how the search method in a Yii2 SearchModel works? I generated it using Gii. Here it is:

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.

Any help would be really appreciated. Thanks.

Paleogeography answered 14/2, 2014 at 12:11 Comment(0)
V
37

The search() function generated by Gii use ActiveRecord::load() to set search parameters :

load() gets the 'FormName' from the model's formName() method (which you may override), unless the $formName parameter is given. If the form name is empty, load() populates the model with the whole of $data, instead of $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 att3 attribute in search() function :

$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, '').

Vic answered 14/2, 2014 at 12:49 Comment(2)
Sometimes simple solutions work; this is one of them! :) Works perfectly.Paleogeography
I'm wondering what is the difference between your code and mine : $search = new ModelSearch(); $search->att3 = 3; $myDataProvider = $search->search(''); I also get the expected result.Bendix
T
0

If you want some additional param to pass to search() method, you can change search method like this in 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,
   ]);
}
Turgid answered 8/6, 2018 at 6:18 Comment(0)
L
0
        $searchModel = new CursadoSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    $dataProvider->query->andWhere([ 'cursado.curso_id' => $curso_id]);
Loge answered 26/6, 2022 at 3:25 Comment(1)
Welcome to Stack Overflow. Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your answer and explain how it answers the specific question being asked. See How to Answer.Everyplace

© 2022 - 2024 — McMap. All rights reserved.