1

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.

Ramisha Mukhtar
  • 321
  • 1
  • 12

2 Answers2

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

Ramisha Mukhtar
  • 321
  • 1
  • 12
  • That's not best solution. This will flush all cache when you access that action which pretty much makes cache useless. Instead you should simply run `yii cache/flush-schema` command when you change DB structure. – Michal Hynčica Feb 17 '22 at 10:58
  • What's the difference? Plus I used it for once only, moreover the application is in development mode on localhost, so what's the big deal? – Ramisha Mukhtar Feb 17 '22 at 11:52
  • 1
    If you only add it, call action and remove it right after that, than it's ok. But it's easy to forget about it and it can get into production. So why take risks when there is cli command for that? Also your answer doesn't mention using it only once and removing it right after that. – Michal Hynčica Feb 17 '22 at 12:19
  • Yes, this was for one-time usage. Edited the answer with the note. Thank you so much ! – Ramisha Mukhtar Feb 18 '22 at 06:21
-1

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

MamadAli
  • 1
  • 3