okainov
okainov

Reputation: 4654

Migrations + model changes in Yii

I want to get some details about recommended work process for Yii. Imagine you already have some database and some model for it. And in one day you need to add a new field to the model. In Django, you can just modify models.py file and then run manage.py makemigrations && manage.py migrate - it will analyze changes, create migration file and apply the changes to the database. But what I should do in Yii?

I see only following way from the docs and manuals:

  1. Create empty migration
  2. Try to write necessary changes in Yii-migration syntax (it may be not so obvious for altering column and adding foreign keys, more difficult than just writing SQL queries).
  3. Run yiic migrate
  4. Generate Model code using Gii for new database structure and copy-paste new fields to your existing Model file.

From my point of view, it leads to lot of useless work by creating migration in addition to modifying Model. So, instead of just modifying model like in Django, I have to use strange migration syntax in Yii and then modify model manually. It it really the way it supposed to work? Isn't it possible to simplify it somehow?

Upvotes: 2

Views: 578

Answers (1)

patriot
patriot

Reputation: 11

I'm using below approach for like 5-6 month and its work perfect:

  1. create new folder inside models folder name it entities.
  2. generate all models you need using gii and

generate all models you need using gii

a) in model path field use new folder, "entities" instead of models folder

b) in model class field, add "Entity" as model name postfix

  1. now in models folder, make new PHP class and named it for example "Gift" and extends it from "GiftEntity"
  2. add new folder, "entities" in preload imported classes.

now, when you make new migration and change your models in db, use gii to regenerate your entity models "GiftEntity", and all your codes in extended model "Gift" are untouched.

Upvotes: 1

Related Questions