Semeena Kabeer
Semeena Kabeer

Reputation: 149

Data can't save after alter the table in yii

I new in yii framework. i create an application in yii framework. i created model, controller, views using gii. After that i alter database table. I deleted 2 column and add 3 new columns. After that overwrite the model using the gii. But when i am trying to save into that table it show property(which was old column that I deleted) is not defined. Plz provide me a solution for this.

Upvotes: 0

Views: 1046

Answers (3)

Dushant Singh
Dushant Singh

Reputation: 49

This is because you are using schema-cache. Your table schema is cached in Yii. You need to flush AR cache. Either flush full schema cache or use Yii::app()->db->schema->getTable('tablename', true); in start of your action. This will update model schema-cache.

Upvotes: 0

Nimir
Nimir

Reputation: 5839

Your changes should also be set at the Views since there are forms, widgets using the old properties !! (for this exact save issue, you will need to fix _form.php which is the partial responsible from your model Save & Update actions.

You can either do the same as you did with the model: (regenerate it using gii) or you can edit it manually (i recommend you get used to this since in the future you will have code you don't want to loose just because of altering a column name. simple Find & edit in most of the text editors will do the job).

May be you need to read a bit more about how MVC works in general & in Yii in special

Upvotes: 0

Stu
Stu

Reputation: 4150

You need to define all columns in the validation rules() method in your model, have a look and make sure that you have defined a rule for every column in the table there, for example (if it's a string with max length 128):

public function rules()
{
    return array(
        ...
        array('myField', 'length', 'max'=>128),
        ...
    );
}

See some info about validation rules.

Also, for forms if you're using CActiveForm widget and calling fields like so:

echo $form->labelEx($model,'myField');
echo $form->textField($model,'myField');

Then you'll need to make sure that a label is defined in the model too, in the attributeLabels() method, for example:

public function attributeLabels()
{
    return array(
        ...
        'myField'=>'My Field',
        ...
    );
}

Lastly, if you want the field to be searchable, you'll need to add a statement to the search() method in the model, for example:

public function search()
{
    ...
    $criteria->compare('myField',$this->myField);
    ...
}

Make sure you have all of those elements present and you shouldn't get the '* is not defined' error.

Also, if you're using schema caching in your main config file, you'll have to clear your cache before the app will see your new database structure.

Upvotes: 1

Related Questions