Reputation: 349
I have a table called calls
and I need a single search input, to search in two columns, contact_number
and contact_name
.
How do I do this on Yii2?
_search.php
$form->field($model, 'searchstring')->textInput(['placeholder' => 'Search']);
common\models\CallsSearch.php
[['searchstring'], 'safe']
(..)
$query->orFilterWhere(['like', 'searchstring', $this->contact_name])
->orFilterWhere(['like', 'searchstring', $this->contact_number]);
controllers\CallsController.php
public function actionIndex()
{
$searchModel = new CallsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Upvotes: 2
Views: 3448
Reputation: 23738
Well you are trying to use the search_string
input to filter 2 fields contact_name
or contact_number
but you are not using it to compare with the fields, you are specifying the custom model attribute as table_column
, which is wrong, you should change the search model code to the following
$query->andFilterWhere ( [ 'OR' ,
[ 'like' , 'contact_name' , $this->search_string ],
[ 'like' , 'contact_number' , $this->search_string ],
] );
Hope it helps.
Upvotes: 8