FuelPHP migrationで接続するDBを指定する

f:id:arcright:20150420175928p:plain基本的に何も指定しなければdefaultのデータベース設定が使用される。

migration全体の設定

マイグレーション - 概要 - FuelPHP ドキュメント
migrationに関する設定はmigrations.php内で行うが、そこにconnectionの設定をするとmigrationで使用するDBを指定することができる。

個別migrationの設定

あまり出番は無いかもしれないが、このmigrationだけ別DBに適応したいという場合があるかもしれない。

普通にoilコマンドでModelをgenerateするとこんなmigrationファイルが作られると思う。

<?php

namespace Fuel\Migrations;

class Create_hoges
{
	public function up()
	{
		\DBUtil::create_table('hoges', array(
			'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true),
			'name' => array('constraint' => 255, 'type' => 'varchar'),
			'age' => array('constraint' => 11, 'type' => 'int'),
			'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('hoges');
	}
}

テーブルの作成,テーブルの削除にはDBUtilクラスのcreate_table,drop_tableを用いている。
これらのメソッドは引数で接続するDBを指定することができる。
DBUtil - クラス - FuelPHP ドキュメント

# テーブル作成
create_table($table, $fields, $primary_keys = array(), $if_not_exists = true, $engine = false, $charset = null, $foreign_keys = array(), $db = null)

# テーブル削除
drop_table($table, $db = null)

$dbのところに接続するdb設定名を入れて上げればよい。(defaultのような