Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Gii afer updating model files

Tags:

yii

yii2

gii

I am learning Yii and I wonder if I generate model and CRUD files using Gii and then I modified these files manually then I changed my database should I run Gii again ? is it going to overwrite my changes in the model files ? if so what is the best solution to update my model , controller and view files to reflect changes in the database without loosing my previous changes ?

like image 824
zac Avatar asked Mar 15 '23 20:03

zac


2 Answers

Gii is designed to provide you basic code at the beginning, then you modify it to fit your needs.

Yes, if you run it again it will override your files (but you need to explicitly check "Override" checkbox).

So the recommended practice is design database structure as fully as possible first and then generate model / CRUD / with Gii.

Then subsequent changes is done manually without Gii.

A little tip: if you run Gii again, you can view changes in diff and manually copy them.

Once you get more experience, most likely you realize that there is no need to do it.

like image 63
arogachev Avatar answered Mar 25 '23 02:03

arogachev


The real solution to your question is to generate all your initial models by Gii, and keep them untouched in a separate directory e.g app\models\base

and name those models as ModelNameBase

Create your own models at app\models e.g ModelName Let the app\models extend the base models.

This way you may modify app\models as much as you want.

Whenever you have a database change, re-generate the base model.

like image 25
Kharbat Avatar answered Mar 25 '23 02:03

Kharbat