我有一个名为 calls
的表,我需要一个搜索输入,以在两列中搜索,contact_number
和 contact_name
。
我如何在 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,
]);
}
最佳答案
您正在尝试使用 search_string
输入来过滤 2 个字段 contact_name
或 contact_number
但您没有使用它来与字段,您将自定义模型属性指定为 table_column
,这是错误的,您应该将搜索模型代码更改为以下
$query->andFilterWhere ( [ 'OR' ,
[ 'like' , 'contact_name' , $this->search_string ],
[ 'like' , 'contact_number' , $this->search_string ],
] );
希望对您有所帮助。
关于php - Yii2:使用一个字段搜索多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49088788/