Iteration..but database schemas changes?

Hi, I was reading Agile Web Application Development with Yii 1.1 and PHP5 and it teaches how to build app from small, with iterations.

However as I was trying out on my own, I was wondering how can I manage database schemas changes as I add more features and functionalities. For eg, I had used Gii to create AR classes initially. But later on when I make changes to tables, how am I suppose to quickly reflect the changes in the AR classes etc?

Depending on how much you changed your model from when it was originally created by Gii, you can just use Gii to generate the model again. After previewing, Gii provides an option to diff the current model code on disk with the newly proposed model code. You can then just manually merge the differences. Same thing for any CRUD views.

My current, albeit somewhat simplistic workflow (with regards to schema changes) is-

[list=1]

[*]Make schema change in database.

[*]Repreview model in Gii.

[*]Use ‘diff’ option in Gii. Manually merge model changes.

[*]If it was a CRUDed model- merge view changes if views are simplistic (otherwise save for a later commit)

[*]Dump DB schema to protected/data/mywebapp.mysql.sql

[*]Commit to SCM.

[/list]

Result is schema changes get tracked with associated model changes.

So it is supposed to be quite manually as I am thinking of really making small completions at first… and then slowly build more and more… :-[

I am searching for same answer. If we have to do it manually after db changed then this is so strange

We’re using GII to generate our models along with the GTC extention:

http://www.yiiframework.com/extension/gii-template-collection/

GTC comes with a template called "FullModel".

Using the FullModel template, if you generate a model for a table "book", it actually creates 2 files:

BaseBook.php

Book.php (extends BaseBook)

BaseBook.php contains all the code generated methods for the model.

You put all your hand made code in Book.php.

When you make changes to your model, you can rerun GII and it just updates BaseBook.php. No hand merging is required.

Yii uses scaffolding pattern which means that it helps you build first, simple, version but you have to extend it on your own. There is some modifications that make updates possible in some simple cases (like BaseModel pattern provided with Giix), where you can regenerate Base class leaving your custom modyfications in descendant class.

Opposite to this approach would be model-driven pattern which needs much more meta data defined for every database field to define editing widget, validation, and so on. In fact it is almost impossible to have such elastic model definition to handle all possible cases, so in real world scaffolding is much better choice.

Don’t belive that there is a framework that will do everything for you :)

It’s not really a big deal to me:

I use migrations, also initially - part of my installer.

Then, at the same time I create the migration, I also edit/add/adjust the corresponding model. And any changes in controller and view.

Migrations and all ties nicely into source control (Mercurial here).

I also ought to do some testing… but was never a TDD guy.

You can use extended Gii (giix) to separate your model from a base model, but I haven’t the need for it.

If you do this incrementally, then your app can grow dynamically.

For our project, we use a combination of the Yii Migrations feature plus the Giix extension.

Here’s how:

  1. All models are generated using Giix to create a BaseModel-Model pair. The base model is NEVER edited. Only put code in the Model class.

  2. All database schema changes are applied via the Yii Migrations feature. Migration scripts are versioned using a CVS such as Subversion or Git.

  3. Once the migration is completed, Only the BaseModel is regenerated. No need to diff the models!

To upgrade a production server to the new version:

  1. Update the source code via CVS (e.g. svn update)

  2. Apply the migrations (.e.g. yiic migrate)

  3. Done!

Notes about the migration scripts:

Since the Migration script is basically a PHP script, it can do a lot more than just changing DB! If the database schema changes affects where certain files are stored (such as images), it can also rename/move those files. Data can also be massaged and transformed accordingly.

It’s such a wonderful tool once you learn how to use it!