dtcyv3985
2018-03-03 20:16
浏览 67
已采纳

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

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,
        ]);
}

图片转代码服务由CSDN问答提供 功能建议

我有一个名为 calls 的表,我需要一个搜索输入来搜索 两列, contact_number contact_name 。 如何在Yii2上执行此操作?

_search.php

  $ form-> field($ model,'searchstring') - > textInput(['placeholder'=>'搜索 “]);  
   
 
 

<强> <代码>常见\模型\ CallsSearch.php

 <  code> [['searchstring'],'safe'] 
 
(..)
 
 $ query-&gt; orFilterWhere(['like','searchstring',$ this-&gt; contact_name])\  n  - &gt; orFilterWhere(['like','searchstring',$ this-&gt; contact_number]); 
   
 
 

controllers \ CallsController .php

  public function actionIndex()
 {
 $ searchModel = new CallsSearch(); 
 $ dataProvider = $ searchModel-  &gt;搜索(Yii :: $ app-&gt; request-&gt; queryParams); 
返回$ this-&gt; render('index',[
'searchModel'=&gt; $ searchModel,
'nataProvider'  =&gt; $ dataProvider,
]); 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doureng5668 2018-03-04 03:56
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报