Yasin Patel
Yasin Patel

Reputation: 5731

How to add column in MODEL in yii2

I have created Model called ExpertQuestion using Gii

After that i added column in table from which i generated Table.

So, my question is how to add column in my ExpertQuestion Model to access it.

Error:

Unknown Property – yii\base\UnknownPropertyException

Setting unknown property: app\models\ExpertQuestion::is_deleted

Upvotes: 0

Views: 3138

Answers (5)

sujoi
sujoi

Reputation: 95

Let's consider the new column name as newCol in your model

update rule function

public function rules()
{   
   return [.....

      [['newCol'], 'string', 'max' => 30],

      ];   
}

Then add labels

public function attributeLabels()
{
        return [...

         'newCol' => Yii::t('app', 'L Name'),

       ];    
}

Now open Search model and update

public function search($params)
{
    ....

    $query->andFilterWhere(['like', 'newCol ', $this->newCol ])

    ....

    return $dataProvider;    
}

Upvotes: 1

Arshad Shaikh
Arshad Shaikh

Reputation: 564

Edit model file and write new field name in rules.

Ex: new field is is_deleted(string) then write new rule for this field.

[['is_deleted'], 'string'],

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133400

This is related at the way yii1 cache the models

If you don't need tu re run Gii for a new generation of model ..

you can simply .. delete the runtime directory of your app

   yourApp/runtime  

Upvotes: 0

Vitaly
Vitaly

Reputation: 1281

Аgain generate the model in Gii, click on the diff button, and copy and paste to your Model that relates to your new field.

screen diff button

Upvotes: 0

Jonnny
Jonnny

Reputation: 5039

Re run Gii for that table and copy and paste the missing information over into your model.

Alternatively if you have no additional code in the model just overwrite the whole model

Or you can use something like Giiant where you have base models that your actual model inherit from, this means you just re run the base model when changes in your DB occur and you don't overwrite any code in your actual model that implements this base model.

Upvotes: 1

Related Questions