Can't get newly added db table columns to show up in Gii

Hi all,

Pretty new to Yii2 here. Super excited about using it, but have been running into an issue I can’t seem to resolve.

I have a table called employees in my database, for which I used Gii to generate the models / controllers / views. All good at first.

Then I needed to add some additional fields. I went ahead and added the new fields using MySQL via the command line, then I tried using Gii to regenerate the models, controllers and views again. But for the life of me, I can’t get them to display! The fields I added were:

address_line_2

postal_code

city

They are in the code that is being generated, but they just won’t display when you visit my … ?r=employees URL.

Here is the model code in models/Employees.php


<?php


namespace app\models;


use Yii;


/**

 * This is the model class for table "employees".

 *

 * @property integer $id

 * @property string $hire_date

 * @property string $first

 * @property string $last

 * @property string $address

 * @property string $address_line_2

 * @property string $postal

 * @property string $phone

 * @property string $deparment

 * @property string $hourly_rate_base

 * @property string $status

 * @property string $title

 * @property string $writeups

 * @property string $city

 */

class Employees extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'employees';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['hire_date'], 'safe'],

            [['hourly_rate_base'], 'number'],

            [['first', 'last', 'city'], 'string', 'max' => 50],

            [['address', 'address_line_2', 'title'], 'string', 'max' => 100],

            [['postal'], 'string', 'max' => 6],

            [['phone'], 'string', 'max' => 16],

            [['deparment', 'status'], 'string', 'max' => 20],

            [['writeups'], 'string', 'max' => 5000],

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'hire_date' => 'Hire Date',

            'first' => 'First',

            'last' => 'Last',

    [b]        'address' => 'Address',

            'address_line_2' => 'Address Line 2',

            'postal' => 'Postal',[/b]

            'phone' => 'Phone',

            'deparment' => 'Deparment',

            'hourly_rate_base' => 'Hourly Rate Base',

            'status' => 'Status',

            'title' => 'Title',

            'writeups' => 'Writeups',

        [b]    'city' => 'City',[/b]

        ];

    }

}



Here is the views/employees/view.php code:





<?php


use yii\helpers\Html;

use yii\widgets\DetailView;


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

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


$this->title = $model->title;

$this->params['breadcrumbs'][] = ['label' => 'Employees', 'url' => ['index']];

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

?>

<div class="employees-view">


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


    <p>

        <?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>

        <?= Html::a('Delete', ['delete', 'id' => $model->id], [

            'class' => 'btn btn-danger',

            'data' => [

                'confirm' => 'Are you sure you want to delete this item?',

                'method' => 'post',

            ],

        ]) ?>

    </p>


    <?= DetailView::widget([

        'model' => $model,

        'attributes' => [

            'id',

            'hire_date',

            'first',

            'last',

  [b]          'address',

            'address_line_2',

            'postal',[/b]

            'phone',

            'deparment',

            'hourly_rate_base:url',

            'status',

            'title',

            'writeups',

 [b]           'city',[/b]

        ],

    ]) ?>


</div>



And here is the views/employees/_form.php code




<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;


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

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

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

?>


<div class="employees-form">


    <?php $form = ActiveForm::begin(); ?>


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


    <?= $form->field($model, 'first')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'last')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'address')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'address_line_2')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'postal')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'phone')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'deparment')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'hourly_rate_base')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'status')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'writeups')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'city')->textInput(['maxlength' => true]) ?>


    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRec

ord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>


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


</div>



Here’s a screenshot with the fields I added missing:

7370

Yii-employees-page.png

Now, if I click the ‘Create Employees’ button, the new fields ARE there. It’s just on this page (r=employees) that I can’t get it to show up!

I have tried clearing all the assets in the web/assets folder, I tried regenerating everything multiple times – no success. I am on a bit of a time crunch and would really appreciate any help you can offer to resolve this issue for me.

Many thanks!

Sam

Ok, I finally figured out which file generates the view that I was having the problem with. It was the "GRID" view, and the file was views/employees/index.php.

Oddly, the newly added fields and several others are commented out! Here’s what the file looks like:




<?php


use yii\helpers\Html;

use yii\grid\GridView;


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

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

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


$this->title = 'Employees';

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

?>

<div class="employees-index">


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

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


    <p>

        <?= Html::a('Create Employees', ['create'], ['class' => 'btn btn-success']) ?>

    </p>

    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'hire_date',

            'first',

            'last',

            'address',

            // 'address_line_2',

            // 'postal',

            // 'phone',

            // 'deparment',

            // 'hourly_rate_base:url',

            // 'status',

            // 'title',

            // 'writeups',

            // 'city',


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>

</div>



Would love to know why! Also not sure why for ‘hourly_rate_base’ there is a ‘:url’ on the end of it.

Sam

Gii make only a stub code, pretty good one, which in many case is production ready but still a stub code.

If the table has many columns and it enable all of them the grid became unreadable.

That’s why the lasts column are commented.

Normally a grid should contain these column that are useful for searching the record and identifying it univocly.

For :URLs this is a formatter, gii try to chose a formatter basing the choice on the column name/type.

Making your own gii template is pretty easy in case you are not satisfied with the default one.

Thanks for the explanation Roberto.

With respect to the :url thing, it does look like a bug, because it’s treating a decimal type column from the db into a URL in the view.

Thanks again,

Sam

The filed name is

hourly_rate_base

and the code that chose the format is:




	public function generateColumnFormat($column)

	{

    	if ($column->phpType === 'boolean') {

        	return 'boolean';

    	} elseif ($column->type === 'text') {

        	return 'ntext';

    	} elseif (stripos($column->name, 'time') !== false && $column->phpType === 'integer') {

        	return 'datetime';

    	} elseif (stripos($column->name, 'email') !== false) {

        	return 'email';

    	} elseif ([b]stripos($column->name, 'url')[/b] !== false) {

        	return 'url';

    	} else {

        	return 'text';

    	}

	}



So rather than a bug I would label it as not enough smart.

That line should be something like:

	} elseif ([b]stripos(&#036;column-&gt;name, 'url')[/b] &#33;== false &amp;&amp; &#036;column-&gt;phpType === 'string') {

But it still would fail in many cases, ie:

url_description

which is a description field and not and url, or

web_link

which is a url and is would be shown as normal text