代码之家  ›  专栏  ›  技术社区  ›  Blackjack

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

  •  1
  • Blackjack  · 技术社区  · 9 年前

    我有一个显示数据的gridView。GridView表有一列称为 ,此列包含 身份证件 使用者 我从中得到的 用户表 . 基本上,此列将数据显示为整数(ID),如下所示:

    enter image description here

    但是我设置了这样的代码来显示 的 身份证件 :

    [
      '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

    创建人必须是整数 “,如下所示:

    enter image description here

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

    谢谢

    使现代化

    这是我的 搜索模型。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 回复  |  直到 6 年前
        1
  •  2
  •   Insane Skull    9 年前
    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;
    }
    
        2
  •  1
  •   ScaisEdge    9 年前

    您应该为列添加适当的筛选器

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