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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么