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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
<?php namespace Fuel\Migrations; class Create_items { public function up() { \DBUtil::create_table('items', array( 'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true), 'name' => array('constraint' => 255, 'type' => 'varchar'), 'created_at' => array('constraint' => 11, 'type' => 'int', 'null' => true), 'updated_at' => array('constraint' => 11, 'type' => 'int', 'null' => true), ), array('id')); } public function down() { \DBUtil::drop_table('items'); } } |
Code after editing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
<?php namespace Fuel\Migrations; class Create_items { public function up() { \DBUtil::create_table('items', array( 'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true), 'name' => array('constraint' => 255, 'type' => 'varchar'), 'created_at' => array('constraint' => 11, 'type' => 'int', 'null' => true), 'updated_at' => array('constraint' => 11, 'type' => 'int', 'null' => true), ), array('id'), true, false, null, array(), 'db_config_key'); } public function down() { \DBUtil::drop_table('items', 'db_config_key'); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
<?php namespace FuelMigrations; class Create_items { public function up() { DBUtil::create_table(Model_Item::table(), array( 'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true), 'name' => array('constraint' => 255, 'type' => 'varchar'), 'created_at' => array('constraint' => 11, 'type' => 'int', 'null' => true), 'updated_at' => array('constraint' => 11, 'type' => 'int', 'null' => true), ), Model_Item::primary_key(), true, false, null, array(), Model_Item::connection()); } public function down() { DBUtil::drop_table(Model_Item::table(), Model_Item::connection()); } } |
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.)