Insert error on new added field

Hello all,

I am new to YII and have a questions about new added fields.

I created a database and then use GII to generate Model, CRUD.

I made some changes on generated files (including Model, Controller and View). Everything is OK.

After that, I added 1 new field to my table and modified to _form view to add new text field.

When inserting, I got error message , the textbox of new field has value.

 Field 'newtextfield' doesn't have a default value

If I re-generate the Model file, it works OK.

How can I add new fields to table without re-generating Model class?


Did you define the newly added column to allow for null values? or not?

My guess is that you defined the column to NOT allow for a null value, and the issue is really about the rules() section of your generated model class. Before you regenerated your model class, your newly added class attribute had no form field validation, so you could submit your form w/o a value for your new field. However, when you try to save it, the database will complain that you are trying to save "null" to a field that will not allow null and no default value for that column was defined.

Once you re-generated your model class, the model generator code was smart enough to recognize the newly added column as being defined to not allow for null values and therefore auto-generated a validation rule so that that field is "required". In this case you cannot submit your form without a value…and with a value, everything saves okay.

But again, this is just a guess.

You can also manually update your models (protected/models), which I recommend doing if you have any custom business logic that you have added.

Thanks for your replies.

I added new fields to rules() array and the problem solved.