2017-10-04 30 views
0

我有一個用於顯示數據的gridView。 GridView的表中有一個是被稱爲創建列,此列包含用戶,我從用戶表得到ID的。基本上,此列會顯示這樣的數據作爲整數(ID):Yii2 Gridview過濾基於varchar作爲輸入值的Int數據類型的列

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

} 

回答

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

這是工作,但我必須首先創建模型關係。因爲我的表格兩者之間沒有關係。感謝:D – Blackjack

+0

但是,如果我有另一列,例如列**電子郵件**,我該如何才能使其工作。該列電子郵件基於用戶ID,就像創建者一樣。我嘗試使用相同的關係,或者只是使用預覽關係。但它不起作用。 – Blackjack

+0

@Blackjack。你想通過電子郵件或用戶名過濾用戶嗎?說明。 –

1

你應該適當的過濾器添加到您的列

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