Prefered Way To Update Models With Db Changes


In Yii 1 I used the Giix extension and found it really helpful because it created a base model folder that all models were derived from. So UserModel.php was derived from baseUserModel.php. This meant that if there were any changes in the User table’s structure or relations with other models you could just run Giix and update the base models, meaning you never lost or overwrote code in your models.

What is the preferred way to do deal with updating models on DB changes in Yii2? I saw a discussion on Git sometime about the inclusion of a giix built into Yii2. But it doesn’t appear to be.

New Yii loves great though



Personally I’d generate models once and then only check diff and copy-paste code pieces but you can do as giix did by entering BaseSomething as model name.

Thanks for your precious answer (as just starting with Yii for a 1-year-or-so project)

DB Tables will change during the lifecycle of the app and

that’s great to know your productive Gii tool can cope with the changes

Thanks 4 Yii !

I bet a similar tool to giix will pop up quite fast. I’m going to migrate all my custom generators for Yii 2 for sure.

I’m not sure that is required in the core Yii.

Hi nineinchnick, you’re right. Since Qiang posted that Yii 2 will be launched with a somewhat stable codebase, I started studying it.

I’ll release an initial version of giix 2 soon.

Thanks everyone

Is there a way to use a prefix when using the model generator for Gii? I’m wanting to use base Models and store them in a models/_base folder and in my actual models folder import the _base/BaseBlog.php into the models/Blog.php and extend it. Just like Gii did.

It’s quicker to just use ‘*’ in the table name option of the model generator. I wanted to prefix all my models in the _base folder with ‘Base’.

So something like:

In the namespace field

app\models\_base\{Base} -> Where {Base} is just a prefix to the table name model being generated?

Because it didn’t seem possible what I was doing I wanted to check if this process is right for creating a set of base models that should I need to change my DB structure during development I can just update these without potentially accidently deleting my model code.

So far I:

  1. Created a new folder called _base within my models folder

  2. created all of my models in Gii and named then "BaseTableName"

  3. Extended my models from the base class like so



* Model extends _base/{BaseModel} similar to Giix



namespace app\models;

class Cart extends \app\models\_base\BaseCart


    // My Model 


  1. Make sure relations reference "BaseSomething" rather than just "Something".

Is that right?

Yes, generate BaseSomething class through Gii and do not change anything.

Make all changes in the extended Something class.

In case in future, you make any changes to the base db table - you can in most cases safely overwrite the BaseSomething class through Gii and those will be available in your Something Class.