Getting unknown property on Model attribute in Yii2-advanced

1k Views Asked by At

I am having a model class in my Yii2-advanced application which was having some attributes.

public function rules()
{
    return [
        [['SESSION_TITLE', 'SESSION_DESCRIPTION', 'TRAINER_ID'], 'required'],
        [['TRAINER_ID','IS_ACTIVE', 'IS_DELETED'], 'integer'],
    ];
}

Now, I need to add an attribute TNI_NUMBER in model which I already have added in database table with similar spellings. After adding in model.

public function rules()
{
    return [
        [['SESSION_TITLE', 'SESSION_DESCRIPTION', 'TRAINER_ID'], 'required'],
        [['TRAINER_ID','TNI_NUMBER' ,'IS_ACTIVE', 'IS_DELETED'], 'integer'],
    ];
}

The form is showing Getting Unknown Property on that specific attribute, on loading the form right after adding this attribute. Note that the data type of attribute in model and in database is not an issue. And the database connection array has set 'enableSchemaCache' => true in it and it can't be set to false.

3

There are 3 best solutions below

4
Ramisha Mukhtar On
Yii::$app->cache->flush();

This worked for me, added it before calling model class in controller action.

NOTE: this is for one-time usage only, once page refreshed after adding this line, do remember to comment or remove it.

0
MamadAli On

you need to refresh database schema

Yii::$app->db->schema->refresh();

You only need to run this once

or

you can set 'enableSchemaCache' to false

0
Hop hop On

My problem was the column was missing in the db table. When I ran migrations it fixed itself.