PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 8, 2022

[FIXED] Pjax Gridview not displaying data in yii2 advanced

 March 08, 2022     gridview, pjax, yii, yii2, yii2-advanced-app     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing