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.
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.
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:
Created a new folder called _base within my models folder
created all of my models in Gii and named then "BaseTableName"
Extended my models from the base class like so
<?php
/*
* Model extends _base/{BaseModel} similar to Giix
*
*/
namespace app\models;
class Cart extends \app\models\_base\BaseCart
{
// My Model
}
Make sure relations reference "BaseSomething" rather than just "Something".
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.