【问题标题】:Yii2 Gridview filtering a column that has Int data type based on varchar as inputed valueYii2 Gridview过滤具有基于varchar作为输入值的Int数据类型的列
【发布时间】:2018-03-16 10:52:41
【问题描述】:

我有一个用于显示数据的 gridView。 GridView 表有一个名为 Created By 的列,该列包含我从 user 表 获得的 UserID强>。基本上,此列会将数据显示为 Integer(ID),如下所示:

但我设置了这样的代码来显示 ID用户名

[
  'attribute' => 'created_by',
  'format' => 'raw',
  'value' => function ($data) {
                $modelUser = Users::find()->where(['id' => $data->created_by])->one();
                $nama = $modelUser->username;
                return $nama;
            },
  'vAlign' => 'middle',
  'hAlign' => 'center',
],

结果

gridView 有过滤列,我正在尝试根据插入的值过滤数据。我正在尝试输入用户名作为过滤值,但它给出了错误消息“created By must be an Integer”,如下所示:

如何使用用户名(varchar)过滤该列?

谢谢

更新

这是我的 searchModel.php

<?php

 namespace app\search;

 use Yii;
 use yii\base\Model;
 use yii\data\ActiveDataProvider;
 use app\models\Product;

 /**
 * ProductSearch represents the model behind the search form of `app\models\Product`.
 */
class ProductSearch extends Product {

/**
 * @inheritdoc
 */
public function rules() {
    return [
        [['productId', 'userId', 'parentId', 'created_by'], 'integer'],
        [['date', 'created_at', 'name', 'address'], 'safe'],
        [['price'], 'number'],
    ];
}

/**
 * @inheritdoc
 */
public function scenarios() {
    // bypass scenarios() implementation in the parent class
    return Model::scenarios();
}

/**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @return ActiveDataProvider
 */
public function search($params) {
    $query = Product::find();

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        return $dataProvider;
    }

    // grid filtering conditions
    $query->andFilterWhere([
        'productId' => $this->productId,
        'price' => $this->price,
        'created_at' => $this->created_at,
        'created_by' => $this->created_by,
    ]);

    $query->andFilterWhere(['MONTH(date)' => $this->date])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'address', $this->address]);

    return $dataProvider;
}

}

【问题讨论】:

    标签: php search gridview filter yii2


    【解决方案1】:

    您应该为您的列添加适当的过滤器

    [
        'attribute'=>'your_attribute',
         ......
         'filter'=>ArrayHelper::map(YourModel::find()->asArray()->all(), 
                       'your_id_column', 'your_name_column'),
    ],
    

    【讨论】:

      【解决方案2】:
      public function rules()
      {
         return [
             [['productId', 'userId', 'parentId'], 'integer'],
             [['date', 'created_at', 'name', 'address'], 'safe'],
             [['price'], 'number'],
             [['created_by'], 'string'],
         ];
      }
      
      public function search($params)
      {
          $query = Product::find()->joinWith('createdBy'); // Whatever relation name
      .
      .
      .
      .
      .
         // grid filtering conditions
         $query->andFilterWhere([
            'productId' => $this->productId,
            'price' => $this->price,
            'created_at' => $this->created_at,
         ]);
      
          $query->andFilterWhere(['MONTH(date)' => $this->date])
              ->andFilterWhere(['like', 'name', $this->name])
              ->andFilterWhere(['like', 'address', $this->address])
              ->andFilterWhere(['like', 'userTableName.username', $this->created_by]); // Whatever table name and field name.
      
      
          return $dataProvider;
      }
      

      【讨论】:

      • 可以,但我必须先创建模型关系。因为我的两张桌子彼此没有关系。谢谢:D
      • 但是,如果我有另一列,例如 email 列,我该如何使它工作。该列电子邮件基于用户 ID,就像创建者一样。我尝试使用相同的关系,或者只使用预览关系。但它并没有奏效。
      • @Blackjack。您想同时通过电子邮件或用户名过滤用户吗?解释一下。
      猜你喜欢
      • 2019-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多