dovhpmnm31216 2017-10-04 22: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-05 05: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条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突