Pjax Gridview 在 yii2 advanced 中不显示数据

Pjax Gridview not displaying data in yii2 advanced

我已经检查了 google 上的许多示例,并做了同样的事情,但还没有运气。 所有功能都像排序和分页一样工作,但只显示 id 字段而不是全部。这是我的资源。

控制器。

<?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)。请帮助我:(

删除属性:

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

来自您的模型。不要覆盖它们,Yii2 会自动从 table 检测它们。删除后 - 它们将出现在您的 GridView.