Changing My Models Often

my model/db design is constantly changing. add column here, delete column there, index here, foreign keys there, you get the idea.

now I use giic to generate the basic CRUD functionality and then add to them. the problem is when i decide to update my model after I modified my CRUD pages. since the model is changed the CRUD pages should also be modified to work properly.

is there a practical way to do this? re-generating the CRUD pages would overwrite my prior modifications. or maybe a clever workaround such as writing my code in a separate class perhaps?

Hi renowijyoyo,

I think one of the things you learn of using Yii is to overthinkg your database design.

When you first start some hours working on the database and construct it in a proper way,

you can avoid a lot of changes. However, I also am familiar with slight changes in the

database. My workflow is to visit gii in the webbrowser and there ‘preview’ the crudding.

Then you can easily see the changes Yii will make, from this view you can easily modify

your cruddings manually.

I know this is not a brilliant workflow, although I do not think there is a better solution,

as Yii may not be aware of what is your manually typed code and what is code of the previous

database designs.

I hope this helps.


You realy should start from a stable database model. Always analyse your project (at least until you have a database that is more or less stable), before you start coding.

Nevertheless the database will change in time, so you need a solution. For little changes in your model (eg adding an attribute) you could use the preview button and copy the changes to your code (without regenerating the file).

A better solution is using a versioning system. This allows you to return to a previous state of your application. For example, you could use github to keep track of your changes in combination with a client like smartgit. It takes some time to get used to this process, but it’s time well spent.

I modified the gii generator to create two files (and two classes) for each table. The second class is a specialization of the first class. For a "user" table that would mean the following classes

"BaseUser" contains the regular stuff gii creates

"User" simply extends BaseUser and has no implementation yet.

Within your application you just use "User". Any changes you want to make you implement in the User class. If you change your DB design you can simply overwrite the BaseUser class without worrying about the changes you made.

using yii does make me spend more time on the DB design however future modification is inevitable.

what i’m doing currently is similar to dave’s.

although not to the extent of modifying gii (not to keen on using gii more than necessary :P). I just replace the controllers base class with my own implementation. this way I get separation between what Gii’s and my own code.