dovhpmnm31216 2017-10-04 14:27
浏览 95
已采纳

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

I've an gridView for showing data. GridView table has a column that called as Created By, this column contain the ID of User that I get from user table. Basicly, this column will show data as Integer(ID) like this:

enter image description here

But I've set code like this to show the username of the 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',
],

Result:

enter image description here

The gridView has filter column, I'm trying to filter data based on inserted value. I'm trying input username as the value for filtering but it give an error message "created By must be an Integer", like this:

enter image description here

How do I can filtering that column using the a username(varchar)?

Thanks

Update

This is my 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条回答 默认 最新

  • drmet46444 2017-10-04 21:32
    关注
    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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部