2017-08-02 172 views
0

我已經檢查了很多谷歌的例子,並做了類似的,但不能得到運氣呢。 所有功能都像排序和分頁一樣工作,但只有id字段顯示,而不是全部顯示。這是我的資源。Pjax Gridview不顯示yii2高級數據

控制器。

<?php 
namespace frontend\controllers; 

use Yii; 
use yii\base\InvalidParamException; 
use yii\web\BadRequestHttpException; 
use yii\web\Controller; 
use yii\filters\VerbFilter; 
use yii\filters\AccessControl; 
use frontend\models\StudentForm; 
//use frontend\models\GridData; 

use yii\data\ActiveDataProvider; 
class StudentController extends Controller 
{ 

    public function actionIndex() 
    { 
     $productsProvider = new ActiveDataProvider([ 
      'query' => StudentForm::find(), 
      'pagination' => [ 
       'pageSize' => 5, 
      ] 

     ]); 
     return $this->render('index', [ 
      // 'searchModel' => $searchModel, 
      'dataProvider' => $productsProvider, 
     ]); 
    } 
} 
?> 

模型

<?php 
namespace frontend\models; 
use yii\base\Model; 

class StudentForm extends \yii\db\ActiveRecord 
{ 
    public $firstname; 
    public $lastname; 
    public $email; 
    public $phone; 


    public static function tableName() 
    { 
     return 'student'; 
    } 

    public function rules() 
    { 
     return [ 
      ['firstname','trim'], 
      ['firstname','required'], 
      ['lastname','trim'], 
      ['lastname','required'], 
      ['email','trim'], 
      ['email','required'], 
      ['phone','trim'], 
      ['phone','required'], 
     ]; 
    } 

    public function getAll() 
    { 
     return static::find()->indexBy('id')->all(); 
    } 

視圖

<?php 
use yii\helpers\Html; 
use yii\grid\GridView; 
use yii\widgets\Pjax; 
?> 
<div class="php-version-index"> 
    <h1><?= Html::encode($this->title) ?></h1> 
    <h2>PHP Versions</h2> 
    <?phpPjax::begin(); ?> 
    <?= GridView::widget([ 
     'dataProvider' => $dataProvider, 
     'columns' => [ 
      ['class' => 'yii\grid\SerialColumn'], 
      'id', 
      'firstname', 
      'lastname', 
      'email', 
      [ 
       'class' => 'yii\grid\ActionColumn', 
      ] 
     ], 
    ]); ?> 
    <?php Pjax::end(); ?> 

的GridView只顯示id領域和其他領域的顯示像(not set)。請幫我:(

回答

1

刪除屬性:

public $firstname; 
public $lastname; 
public $email; 
public $phone; 

從你的模型不要忽略他們,Yii2將自動從表中檢測到它們取出後 - 他們會出現在你的GridView

+0

。這是工作,但爲什麼他們被覆蓋? –

+1

活動記錄屬性是以區分大小寫的方式在關聯的表列之後命名的,Yii自動爲活動記錄中關聯表的每一列定義一個屬性,不應該重新聲明任何屬性來避免這種事情。 – Yupik