我在我与用户建立联系的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]);

最佳答案

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

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

关于php - 一栏搜索全名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35797042/

10-13 03:19