0
votes

In my controller in yii2 I have the following:

$searchModel = new HealthSearch();
$dataProvider = $searchModel->search(['HealthSearch'=>['zip'=>$zipcode]]);

which works but I would like it to also search for zipcodes and speciality

I tried:

$dataProvider = $searchModel->search(['HealthSearch'=>['zip'=>$zipcode,'pri_spec'=>$sspec']]);

but that does not work? What is the correct way of searching??

1
you have a php syntax error. remove the last ' .Salem Ouerdani
Show us your search model codesoju
Apart from the typo, the code looks correct. You usually don't create that array yourself, it comes from the HTTP request: $dataProvider = $searchModel->search(Yii::$app->request->queryParams).Beowulfenator

1 Answers

0
votes

After removing the last single quoted '. Your code should work fine. An easier to read version may look like :

$searchByAttr['HealthSearch'] = [
    'zip' => $zipcode,
    'pri_spec' => $sspec
];

$dataProvider = $searchModel->search($searchByAttr);

Also you need to check the HealthSearch class which is the first responsible of making that search. Gii generates a primary boilerplate from your model that need to be adapted to your app in further steps. By default the HealthSearch::search() method should filter by all model's safe attributes and as any ActiveRecord class it has also a rules() method that returns those safe attributes. So if zip and pri_spec are not included in that array they will be simply ignored.