how to add new fields in table yii2

HI all,

I have created a form and table using yii and everything auto generated.Now I want to remove and add extra fields to table. What are the steps I should do. I just added manually fields to table but its not reflecting. I use yii2


If you don’t have change anything in your code after creating CRUD using Gii module then do it again.

I have done lot of changes in the code. I need to preserve all the codes.

Should I manually add fields ? I am also able to see

  • @property integer $id

  • @property string $name

in model php file.

Basically I need to add website field into the table. I understand I need to add property and table field in mysql table.Please correct me if I am wrong.

Please help

any one have idea ?

Basically you have to modify your existing code BY HAND when you have modified the table schema after you had added lots of changes to the Gii-generated skeleton.

One thing that might help you is a "diff" feature of Gii. It will tell you the different parts of code between the existing file and one to be generated. Just preview the changes, and copy the newly added lines to paste them to you current source files. Well, it might be a tedious job, however. You have to check line by line to ensure the consistency of your code.

Good luck!


I am facing an issue

I have relation in existing table but these relations are not generating from table. I checked other tables too but hasone and has many relation not generating by gii tool.

If you put the relation after creating the GII model then it will not reflect to your existing model …And you have to create it by your own…

And in case of the adding any new field to the table then you have to put that particular attribute to "MODEL" "SEARCH MODEL" and "Controller"(if required)…specifically you have to made change the rules and searching query… And one more place i.e. in "_from.php"…

And for you “hasOne” & “hasMany” issue…you have to check that whether you create all the related table’s model…cause you have to create all of those otherwise it cannot create the relation between them…

I think now you will not get any issue…

I have all relation correctly in table. I am working on a maintenance project here I am able to see has one and has many relation that means all relation is correct but when I clear has one and has many from model file. It doesn’t regenerate. I hope you got an idea.

Well then, Another thing you should check that whether "Build Relation" is checked…cause if its not checked then relations will not be generate…7280

model generate.png