Database Question

I’m getting started with grasping the whole MVC paradigm and had a question about how coders deal with database schema changes. In a classic single tier app, adding new fields is almost a two step process: change the database, update a couple lines of code. But it seems with yii, you change the database, then run the yiic tool, then modify the code. But does all that old code get overwritten by yiic? What’s the normal workflow … do I keep a backup and then compare and add in the code from the old version I want to keep?

Sorry if this is a silly question, but I want to keep all my ducks in a row before I begin building my app.

Thanks in advance!

No, you don’t rerun the yyic tool unless you add a new table. When adding a column AR will be aware of that from the db schema.

You will typically add an input field in some form, perhaps declare an attributeLabel and probably a validation rule for the new attribute.


Wow, sounds pretty flexible. The comments section and the rules(), relations(), and attributeLabels() functions of the generated models code got me a little worried (they list out available columns). Is there a checklist of places to modify when the schema changes? Or is it just the the model and view files I should worry about?

Thanks for the quick reply.

rules() is the place where you add validation rules.

attributeLabels() is only used if you want display labels other than the attribute names. It’s also where you would prefer to add t() method calls for label translation.

relations() doesn’t change if you just add a column to the db. If you add related tables you may want to define new relationship(s).

I forgot to mention safeAttributes() where you should add the new attribute. Thus it will be massively assigned in the controller. For the basic generated CRUD I don’t see the need for changing anything in the controller.

If you add a table you will run yiic on it and get the new model, the corresponding controller with CRUD actions as well as a new directory with the CRUD views.