problem passing parameters to search form

Struggling to pass a parameter (groupid) to the search form to be included as ‘where’ in the database search.

Its already defined in the model.

I get an error here "Undefined variable search




<?php


//MembersController.php

search(Yii::$app->request->queryParams); 

$searchModel = $search->search(['searchModel'=>['groupid'=>$groupid]]); 

return $this->render('index', [ 'searchModel'=> $searchModel, 'dataProvider' => $dataProvider, ]); 

} 


//Members model

public function rules() { 

  return [ [['groupid'], 'integer'], 

  //....other variables ]; 

} 


public function attributeLabels() { 

   return [ 'groupid' => 'Umati Africa Group #.', 

   //....other variables ]; 

} 


//MembersSearch.php (search model) 

public function search($params) { 

   $query = Members::find(); 

   $dataProvider = new ActiveDataProvider(['query' => $query,]); 

   if (!$this->validate()) { 

      return $dataProvider; 

   } 

   $query->andFilterWhere([ 'userid' => $this->userid, 

   'groupid' => $this->groupid, ]);       

  

   $query->andFilterWhere(['like', 'name', $this->name])

         ->andFilterWhere(['=', 'groupid', $this->groupid]);    

   return $dataProvider; 

} 



Struggling to pass a parameter (groupid) to the search form to be includes as ‘where’ in the database search I get an error here "Undefined variable search

groupid is defined in your model?

If not firstly define it as a class variable and add it to Yii filter

@Grischer

Awesome. I couldn’t even try to read the code …

@saiyalel

Would you please make your post readable using proper markups?

@Softark

that is the automatic quote of saiyalel post, not code written by me ;)

I don’t understand this code snippet. But usually you don’t have to touch the controller code.




    public function actionIndex()

    {

        $searchModel = new MembersSearch();

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

        return $this->render('index', [

            'searchModel'=> $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }



‘groupid’ is already taken into consideration in these parts of your search model.




    $query->andFilterWhere([

        'userid' => $this->userid, 

        'groupid' => $this->groupid,

    ]); 



And




    $query->andFilterWhere(['like', 'name', $this->name])

        ->andFilterWhere(['=', 'groupid', $this->groupid]);    



Yeah, I knew it. :)




  

	//solved as below 		//MembersController 		$searchModel = new MembersSearch(); 		$dataProvider = $searchModel->search(Yii::$app->request->queryParams,$groupid = 'groupid'); 		//MembersSearch Model 		$query = Members::find()->where(['groupid' => $params['groupid']]); 






My solution is:




$searchModel = new YormodelSearch(['AField'=>'AValue', 'AnotherField'=>'AnotherValue']);

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