Issue
I have checked many examples on google and do same like that but can not get luck yet.
All functionalities are worked like sort and pagination, but only id
field is displaying instead of all. Here is my resources .
Controller.
<?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,
]);
}
}
?>
Model
<?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();
}
view
<?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 displaying only id
field and other field displaying like (not set)
. Please help me :(
Solution
Remove attributes:
public $firstname;
public $lastname;
public $email;
public $phone;
From your model. Don't override them, Yii2 will automatically detect them from table. After removing - they will appear in your GridView
.
Answered By - Yupik
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.