在一个columnsearch中搜索fullname

时间:2016-03-04 13:15:22

标签: php yii2

我在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]);

2 个答案:

答案 0 :(得分:5)

在您的模型博客中为fullName添加一个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中,您可以直接使用fullName字段

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

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

对于姓名和姓氏,请同时尝试添加

   ->orFilterWhere(['like', 'concat(UrUser.Name, " " , UrUser.Surname) ', $this->fullName]);

答案 1 :(得分:0)

尝试类似

的内容

docker run