我在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)
。请帮帮我:(
答案 0 :(得分:1)
删除属性:
public $firstname;
public $lastname;
public $email;
public $phone;
从你的模特。不要覆盖它们,Yii2会自动从表中检测它们。删除后 - 它们将显示在您的GridView
。