【问题标题】:How to Concatenate SQL queries of Multiple Columns of Gridview into one column for search and filter?如何将 Gridview 多列的 SQL 查询连接成一列进行搜索和过滤?
【发布时间】:2018-01-25 07:16:08
【问题描述】:

假设我在 gridview 中有 5 列合并为 1 列,我想像这样连接列过滤器功能

SELECT strcat(Column1, Column2, Column3, Column4, Column5) as MainColumnName.

当我搜索时,我必须执行一些这样的查询,

WHERE MainColumnName LIKE '%userinput%';

问题:如何在yii2中实现,查询的结果会在gridview的下拉过滤器中体现出来。

提前致谢,

【问题讨论】:

  • 有人帮忙吗?

标签: php gridview yii2 yii2-advanced-app yii2-basic-app


【解决方案1】:

最简单的方法是扩展搜索功能的功能,以便您可以在 dataProvider 中使用结果

为此你应该

正确设置模型以获得串联结果,例如使用 getter

  /* Getter for  full name */
  public function getMainName() {
      return $this->column1 . ' ' . $this->column2 . ' ' . $this->column3 . ' ' . $this->column4 . ' ' . $this->column5;
  }

  /* Your model attribute labels */
  public function attributeLabels() {
      return [
          /* Your other attribute labels */
          'mainName' => Yii::t('app', 'Main Column  Name')
      ];
  }

为过滤和排序设置搜索模型

  /* your calculated attribute */
  public $mainName;

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

  /**
   * setup search function for filtering and sorting 
   * based on mainName field
   */
  public function search($params) {
      $query = YourModel::find();
      $dataProvider = new ActiveDataProvider([
          'query' => $query,
      ]);

      /**
       * Setup your sorting attributes
       * Note: This is setup before the $this->load($params) 
       * statement below
       */
      $dataProvider->setSort([
          'attributes' => [
              'id',
              'mainName' => [
                  'asc' => ['column1' => SORT_ASC, 'column2' => SORT_ASC, 'column3' => SORT_ASC, 'column4' => SORT_ASC,  'column5' => SORT_ASC],
                  'desc' => ['column1' => SORT_DESC, 'column2' => SORT_DESC, 'column3' => SORT_DESC, 'column4' => SORT_DESC,  'column5' => SORT_DESC],
                  'label' => 'Full Name',
                  'default' => SORT_ASC
              ],
              'other_your_column'
          ]
      ]);

      if (!($this->load($params) && $this->validate())) {
          return $dataProvider;
      }

      ........

      /* Setup your custom filtering criteria */

      // filter by person full name

      $query->andWhere(" concat(column1, ' ', column2, ' ',column3, 
             ' ',column4, ' ',column5) 
                   LIKE concat('%','" . $this-mainName . "',  '%') ");

      return $dataProvider;
  }

在你的 gridView 中

  echo GridView::widget([
      'dataProvider' => $dataProvider,
      'filterModel' => $searchModel,
      'columns' => [
          ['class' => 'yii\grid\SerialColumn'],
          'id',
          'mainName',
          ['class' => 'yii\grid\ActionColumn'],
      ]
  ]);

您可以在http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/ 中找到一些示例

【讨论】:

  • 得到这个错误,语法错误,意外'',platformId,''(T_CONSTANT_ENCAPSED_STRING),期望','或')'我的sql查询看起来像这样:$query->andWhere('concat (classId, ' ',platformId, ' ',familyId, ' ',subFamilyName, ' ',variantName) LIKE "%' . $this-fullNamesCombo . '%"')
  • $this-fullNamesCombo 缺失 > $this->fullNamesCombo
  • 已添加 > 还是一样:|
  • 用不同的引号顺序更新答案,并为like 连接
  • 现在出现此错误,SQLSTATE[42703]: Undefined column: 7 ERROR: column "classid" does not exist 提示:也许你的意思是引用列 "storeNames.classId"。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
  • 2017-02-19
相关资源
最近更新 更多