Yii2 Gridview过滤一个列,该列具有基于varchar的Int数据类型作为输入值

时间:2017-10-04 22:27:54

标签: php search gridview filter yii2

我有一个用于显示数据的gridView。 GridView表格中有一个名为创建者的列,此列包含用户 ID ,我从用户表获取 STRONG>。基本上,此列将数据显示为整数(ID),如下所示:

enter image description here

但是我已经设置了这样的代码来显示 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',
],

结果

enter image description here

gridView有过滤列,我试图根据插入的值过滤数据。我尝试使用输入用户名作为过滤值,但它会给出错误消息" 创建者必须是整数",如下所示:

enter image description here

如何使用用户名(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;
}

}

2 个答案:

答案 0 :(得分:1)

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;
}

答案 1 :(得分:1)

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

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