Doctrine-Like Db Migration

Greetings, Yiiers!

I used to work in Yii for a while, but now have switched to symfony (not by choice). Symfony is awful and I hate it, but it is tremendously powerful. The thing I love most about it is the simplicity of switching between different DB schemas (yes, I’m aware that this is doctrine and not symfony). For anyone who hasn’t used symfony or doctrine, let me explain in a few words what it’s all about.

Say you’re working on a new feature for your web app, which involves changing DB structure. Maybe you added a new table, removed a column here, added one there etc. But this feature takes time to develop, and while you’re working on it, you keep getting high priority bugfixes that you need to handle immediately. No problem, if you’re using git, you can easily switch between branches, but what about DB schema? You’re not gonna change it manually every time you switch between branches? Well, with doctrine, this is just as easy. You run this simple command:

php app/console doctrine:schema:update --force

and the DB schema is instantly modified to match the state of your DB Entities (in doctrine, you define your DB schema by creating Entity classes with column definition). Now you can switch back and forth between different branches easily.

Now, before someone shouts at me and tells me that I should use doctrine if I think it’s better than Yii’s ORM, let me also say that I hate everything else about doctrine, apart from this lovely little feature. The thing I hate the most about it is that Entities are completely isolated from the app and aren’t able to do any serious business logic at all - you basically use them for storage only. Symfony guys will, of course, give you a lecture about dependency injection and will try to persuade you that you’re not actually supposed to put business logic in Entities at all. Great, so where do I put it? In controllers? How stupid is that? This is why I prefer Yii’s ORM. All the business logic can be encapsulated in DB Models.

So, finally, on to the question. Is there a way to achieve something like this in Yii? I remember that even Yii 1.1 had support for database migrations, but I never had time to get into that. In any case, it doesn’t seem nearly as easy as doctrine’s schema management. Will Yii 2 have something that is this simple and effective?

If you don’t care about data and all your schema is in migrations then you can simple delete database and re-run migrations.