php - Yii2:使用一个字段搜索多列

标签 php yii2

我有一个名为 calls 的表,我需要一个搜索输入,以在两列中搜索,contact_numbercontact_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_namecontact_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/

相关文章:

javascript - 如何将ajax值传递给同一页面上的php变量?

php - “PDOException”语法错误或访问冲突 : 1064 You have an error in your SQL syntax; check

php_mongo 扩展已经安装了,但是为什么 rockmongo 仍然没有连接?

php - Eclipse 和 Xdebug 不解析/etc/php5/conf.d 中的附加 ini 文件

php - 获得连接表 Yii 2 的值(value)

yii - 更新模型文件后使用 Gii

mysql - 在 yii2 中编写连接查询

javascript - 如何在 PHP 文件中使用 MySQL 检查数据提交是否成功

PHP 命名空间 : Function Loading

php - Yii2 连接多个表的关系