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
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