2016-03-04 132 views
0

我在我的BlogSearch中查询,我加入与用户的关系。在我的专栏用户中,我想通过他的全名搜索用户。这是我的BlogSearch代码。在一个列搜索全名搜索

$query->joinWith('relUser'); 

     $query->andFilterWhere([ 
      'Id' => $this->Id, 
      'CreatedAt' => $this->CreatedAt, 
     ]); 


$query->andFilterWhere(['like', 'Title', $this->Title]) 
      ->andFilterWhere(['like', 'Description', $this->Description]) 
      ->andFilterWhere(['like', 'User.Name', $this->Rel_User]) 
      ->andFilterWhere(['like', 'User.Surname', $this->Rel_User]); 
+0

什么意思不工作,,错误,,错误的结果?..请显示所有的搜索模型 – scaisEdge

+0

我怎样才能将我的列连接成一个? – qwerty

+0

你想在哪里连接列,,, sqlquery? ,,, gridview列)?更正你的问题,并有适当的解释,并显示你的相关searchModel,controllerAction和查看请.. – scaisEdge

回答

3

在模型博客添加getter的全名

public function getFullName() { 
    return $this->relUser.>Name . ' ' . $this->relUser.Surname; 
} 

添加到您的BlogSearc一个字段过滤

class BlogSearch extends Blog 
{ 

    public $fullName; 

/* setup rules */ 
public function rules() { 
    return [ 
    /* your other rules */ 
    [['fullName'], 'safe'] 
    ]; 
} 

然后用这个查询,而不是你的JoinWith

 $query->joinWith(['relUser' => function ($q) { 
      $q->where('UrUser.Name LIKE "%' .$this->fullName . '%"' . ' OR UrUser.Name LIKE "%' . $this->fullName . '%"'); 
     }]); 

,并在你的GridView可以在同一时间尝试添加

->orFilterWhere(['like', 'concat(UrUser.Name, " " , UrUser.Surname) ', $this->fullName]); 
直接使用全名外地

<?= GridView::widget([ 
     'dataProvider' => $dataProvider, 
     'filterModel' => $searchModel, 
     'columns' => [ 
      'Title', 
      'Description', 
      'fullName', 
      //'CreatedAt', 
      // 'IsDeleted', 

      ['class' => 'yii\grid\ActionColumn'], 
     ], 
    ]); ?> 

对于名和姓

+0

我有数据库异常:SQLSTATE [42S22]:未找到列:1054未知列'relUser 'Name'in'where clause' 正在执行的SQL是:SELECT COUNT(*)FROM'urBlog' LEFT JOIN'urUser' ON'urBlog'.'Rel_User' ='urUser'.'Id' WHERE relUser.Name LIKE “%%”或者relUser.Surname LIKE“%%”这个异常是这个查询的形式。你可以在这个查询中告诉我'relUser.Name','relUser'是关系名称或者表名吗? – qwerty

+0

在$ query-> anWhere(...)中,您必须设置正确的表名...可能是UrUser ..我已更新答案.. – scaisEdge

+0

不幸的是,搜索不起作用时,我把一些字母它notting发生了 – qwerty

0

尝试像

$query->select('id, CONCAT_WS(' ', Name, Surname) as name') ->from('customer') ->where('CONCAT_WS(' ', Name, Surname) LIKE "%' . $search .'%"') ->limit(10)