Problems displaying an array within a GridView

I’m new to Yii, and I want to display an array in a GridView. The array is as follows:


public $tablesList = [

        [

            'tablename' => 'acctypes',

            'tabledesc' => 'Account types',

        ],

        [

        'tablename' => 'accounts',

        'tabledesc' => 'Account',

        ],

        [

        'tablename' => 'financeinst',

        'tabledesc' => 'Finance institutions',

        ],

        [

        'tablename' => 'imppages',

        'tabledesc' => 'Import pages',

        ],

        [

        'tablename' => 'imptrans',

        'tabledesc' => 'Imported transactions',

        ],

        

    ];



First of all, I created a model, Tables, as follows:


<?php


namespace app\models;


use Yii;


/**

 * This is the model class for the list of database tables

 *

 */

class Tables extends \yii\base\Model

{

 

    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['tablename'], 'string', 'max' => 100],

            [['tabledesc'], 'string', 'max' => 100],

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'tablename' => 'Table name',

            'tabledesc' => 'Description',

        ];

    }


 }



Then I created a TablesSearch class, which extends Tables:


<?php


namespace app\models;


use Yii;

use yii\base\Model;

use yii\data\ArrayDataProvider;

use app\models\Tables;


/**

 * AcctypesSearch represents the model behind the search form about `app\models\Acctypes`.

 */

class TablesSearch extends Tables

{

    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['tablename'], 'string', 'max' => 100],

            [['tabledesc'], 'string', 'max' => 100],

        ];

    }

    

    /**

     * @inheritdoc

     */

    public function scenarios()

    {

        // bypass scenarios() implementation in the parent class

        return Model::scenarios();

    }


    /**

     * Creates data provider instance with search query applied

     *

     * @param array $params

     * @param array $models

     *

     * @return ArrayDataProvider

     */

    public function search($params, $models)

    {

        $dataProvider = new ArrayDataProvider([

            'allModels' => $models,

            'sort' => [

                'attributes' => ['tablename', 'tabledesc'],

            ],

            'pagination' => [

                'pageSize' => 50,

            ],

        ]);

        

        if (!($this->load($params) && $this->validate())) {

            return $dataProvider;

        }


        return $dataProvider;

    }

}



Then, I used gii to generate a controller class for the Tables model:


<?php


namespace app\controllers;


use Yii;

use app\models\TablesSearch;


class TablesController extends \yii\web\Controller

{

    public $tablesList = [

        [

            'tablename' => 'acctypes',

            'tabledesc' => 'Account types',

        ],

        [

        'tablename' => 'accounts',

        'tabledesc' => 'Account',

        ],

        [

        'tablename' => 'financeinst',

        'tabledesc' => 'Finance institutions',

        ],

        [

        'tablename' => 'imppages',

        'tabledesc' => 'Import pages',

        ],

        [

        'tablename' => 'imptrans',

        'tabledesc' => 'Imported transactions',

        ],

        

    ];

	

    public function actionIndex()

    {

        $searchModel = new TablesSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $this->tablesList);


        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

            }


}



Note that the array is instantiated as a member of the TablesController class.

I then tried to create a view to display the array. First of all, I created apps\views\tables\index.php:


<?php


use yii\helpers\Html;

use yii\grid\GridView;


/* @var $this yii\web\View */

/* @var $searchModel app\models\TablesSearch */

/* @var $dataProvider yii\data\ArrayDataProvider */


$this->title = 'Application Tables';

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="tables-index">


    <h1><?= Html::encode($this->title) ?></h1>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>


    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            'tablename',

            'tabledesc',

        ],

    ]); ?>


</div>



When I try to display the page (from index.php?r=tables), I get the following error:

I tried creating a _search.php file within apps\views\tables, as set out below, but to no avail:


<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;


/* @var $this yii\web\View */

/* @var $model app\models\TablesSearch */

/* @var $form yii\widgets\ActiveForm */

?>


<div class="tables-search">


    <?php $form = ActiveForm::begin([

        'action' => ['index'],

        'method' => 'get',

    ]); ?>


    <?= $form->field($model, 'tablename') ?>


    <?= $form->field($model, 'tabledesc') ?>


    <div class="form-group">

        <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>

        <?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>

    </div>


    <?php ActiveForm::end(); ?>


</div>



Can anyone tell me where I’m going wrong here? Or can anyone post an example of code that displays an array successfully within the Yii 2 framework?

Why an array? This data looks like it should live in a database. If it does then gii does everything for you.

Otherwise, I would move $tablesList into the search model and do the search there eg,




    public function search($params)

    {

        $dataProvider = new ArrayDataProvider([

            'allModels' => $this->models,

            'sort' => [

                'attributes' => ['tablename', 'tabledesc'],

            ],

            'pagination' => [

                'pageSize' => 50,

            ],

        ]);

        

        if (!($this->load($params) && $this->validate())) {

            return $dataProvider;

        }


        return $dataProvider;

    }




1 Like

I tried moving the $tablesList array into the TablesSearch class as Flarpy suggested, but I’m still getting the same error.

All I want to do is to display the array contents in a grid or table. I’m new to Yii, so I know I’m making some basic mistake here. Can someone please help me out on this?? Surely such a simple task can’t be that difficult. A complete, working example of Yii2 code that displays an array in a GridView would be really helpful.