2017-07-14 67 views
1

下面是我在用戶模型擴展的UserSearch模型的搜索功能中使用的數據提供者查詢。我試圖在gridview中顯示'entries'的值,但無法訪問'entries'列。請幫忙。In Yii2無法在gridview中顯示模型中的自定義列

 $query = User::find() 
     ->select("users.Id, users.FirstName, users.LastName, users.EntryId, GROUP_CONCAT(entry.EntryId ORDER BY entry.Id SEPARATOR '<br>') as entries")   
     ->join('LEFT OUTER JOIN','entry','entry.CustomerEntryId = users.EntryId') 
     ->where(['users.IsActive' =>1]) 
     ->groupBy('users.Id'); 

    $dataProvider = new ActiveDataProvider([ 
     'query' => $query, 
    ]); 

用戶模型代碼

class User extends \yii\db\ActiveRecord 
{ 
    public $password_repeat; 
    public static function tableName() 
    { 
     return 'users'; 
    } 

    public function rules() 
    { 
     return [ 
      [['FirstName','LastName','EmailId','MobileNo','Password','password_repeat'], 'required'], 
      ['EmailId', 'email'], 
      [['EmailId'], 'unique'], 
      ['password_repeat', 'compare', 'compareAttribute'=>'Password', 'message'=>"Passwords don't match", ], 
      ['MobileNo', 'is8NumbersOnly'],   
     ]; 
    } 

    public function is8NumbersOnly($attribute) 
    { 
     if (!preg_match('/^[0-9]{8}$/', $this->$attribute)) { 
      $this->addError($attribute, 'must contain exactly 8 digits.'); 
     } 
    } 
} 

GridView控件代碼

echo GridView::widget([ 
    'dataProvider'=>$dataProvider, 
    'filterModel'=>$searchModel, 
    'showPageSummary'=>true, 
    'pjax'=>true, 
    'striped'=>true, 
    ], 
    'panel'=>['type'=>'primary', 'heading'=>'Members Report'], 
    'columns' => [ 

      [ 
      'label'=>'First Name', 
      'attribute'=>'FirstName', 
      'value'=>'FirstName', 
      ], 

      [ 
      'label'=>'Email Id', 
      'attribute'=>'EmailId', 
      'value'=>'EmailId', 
      ], 

      [ 
      'label'=>'Entries', 
      'attribute'=>'Entries', 
      'value'=>'entries', 
      ], 

    ], 
]);  
+0

節目還您GridView的代碼..和用戶模型 – scaisEdge

回答

1

用於管理計算列簡單的方法就是在你的模型類中添加一個公共變種(在這種情況下,用戶模式)

class User extends \yii\db\ActiveRecord 
{ 
    public $entries 

the n的你GridView控件,你可以簡單地添加這個名字

'columns' => [ 
     ['class' => 'yii\grid\SerialColumn'], 
     .... 
     'entries', 
     ..... 
+0

謝謝你,我的錯誤是我在我的搜索模型增加了公共變種列:) –

相關問題