一列搜索全名

qwe*_*rty 1 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]);
Run Code Online (Sandbox Code Playgroud)

sca*_*dge 5

在您的模型Blog中,为fullName添加一个吸气剂

public function getFullName() {
   return $this->relUser.>Name . ' ' . $this->relUser.Surname;
 }
Run Code Online (Sandbox Code Playgroud)

向您的BlogSearc添加一个要过滤的字段

class BlogSearch extends Blog
{

  public $fullName;

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

然后使用此查询而不是您的JoinWith

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

在您的gridView中,您可以直接使用fullName字段

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

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
Run Code Online (Sandbox Code Playgroud)

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

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