Tag Archives: [:ja]複数データベース[:en]Multi Database[:]

FuelPHP migration to multi databases


I’ll explain the way to migration to multi databases in FuelPHP 1.7.2.

In FuelPHP, php oil g model item name:string generate codes of model and migration. And php oil g scaffold ... also generates migration code. Add some essences into the migration code to be generated, and you will handle with multi databases.

Code before editing

Code after editing

I added db_config_key. This parameter specifies which database to use. Specifically, the parameter value is the key described at config/db.php. For example, 'default' is the value of the parameter. If you don’t pass any value, 'default' will be used.

In such way, you can use multi databases, with indicate database to use for each migration code.

Create migration code with Model method

Let’s try to utilize Model methods.

It supposes Model_Item inherits Orm. Model_Item::connection() returns database key, Model_Item::table() returns table name, Model_Item::primary_key() returns primary key column name so you can utilize these methods for migrating if you set table name, database and primary key in model code. And in this way, you don’t have to change migration code when you change table name and so on. (But attention when you change about table name or $_connection of existing model, in db rollback.)