How to patch tables for I18n from a reverse-engineered database
If you've ever wanted to construct tables using I18n, but didn't want to fiddle around with YAML and reverse-engineer the database to visualize it, then you can now patch your visually-generated database (either using !DBDesigner4 or whichever tool you're using) using a simple Pake task.
Generally, the cycle of generating the proper model is syncing the visual database with the server, reverse-engineering it using Propel (symfony propel-build-schema), building the model (symfony propel-build-model) and clearing the cache (symfony cc). While this is all fine and dandy if you're not using I18n, there's a show stopper - you can modify the generated schema.yml file to use I18n features, but all of those settings get wiped as soon as you restart the visual-db syncing (since the YAML gets overwritten).
The solution is to have an intermediate step in the cycle - the patch-db-i18n. Basically it's just a Pake task that takes the generated schema.yml and applies the transformation necessary for the tables to have I18n capability.
To use this Pake task, copy the appended file to your PEAR data directory: $PEAR/data/symfony/tasks. This will make the file available in the framework for you to use the Pake task. The modified cycle is now:
- sync your visual database to the database server
- reverse-engineer the database: symfony propel-build-schema
- run the database patch: symfony patch-db-i18n <application> [<suffix> [<column>]]
- rebuild the model: symfony propel-build-model
- clear the cache: symfony cc
That's it. Your database should now be I18n enabled. There are a few gotchas, though...
symfony patch-db-i18n myapp _trans lang_id
This will match any pair named (*, *_trans) and apply I18n _attributes to them and use the lang_id column in the child tables (ie, *_trans) for the isCulture column.
The parameters for the Pake task are:
- <application> - the name of an application in your project (it uses it to provide sfContext)
- <suffix> - the appended string used to identify parent/child I18n tables (only suffixes are supported, if none given, defaults to "_i18n")
- <column> - the culture-enabled column used to identify the culture/language (must be VARCHAR(7), a primary key and required, if no value given, defaults to "culture")
- You must use a relational database with references. One of the most common fatal errors when generating the model is the result of missing relational references. To assure maximum reliability, use relational references and (if using MySQL) use InnoDB as the storage engine.
- You cannot use table name pairs other than * and *suffix, which is a limitation of the parametrized Pake task.
As a Plugin
There is also a modified version of sfPakePatchDbI18n.php that enables you to install it as a plugin into your Symfony framework, and you don't have to specify an application to run this patch, simply run it like symfony patch-db-i18n [<suffix> [<column>]].
symfony patch-db-i18n _trans lang_id
To use this plugin, unpack the appended file sfPakePathDbI18n.tar.gz into your plugins dir and use it as the example above.
This plugin worked only in symfony 1.0.
I transformed that pluggin into a task of symfony 1.1.
Put the file doPatchDbI18nTask.class.php to lib/task/doPatchDbI18nTask.class.php
symfony propel:build-schema symfony mp:patch-db-i18n symfony propel:build-model
- sfPakePatchDbI18n.php (4.4 kB) -
patch-db-i18n Pake task, added by klemen on 05/09/07 10:21:58.
- sfPakePatchDbI18n.tar.gz (2.4 kB) -
Plugin sfPakePatchDbI18n, for those who need it as a plugin, for putting into 'sf_dir/plugins'., added by asb on 02/13/08 15:15:30.
- sfPakePatchDbI18n.2.php (4.6 kB) -
add this attribute "baseClass: i18nBaseClass" to i18n tables, added by trisoft on 07/02/08 10:51:04.
- doPatchDbI18nTask.class.php (3.3 kB) -
Pluggin turned into a task to work with the symfony 1.1, added by patrickespake on 07/16/08 15:33:24.