◯ 「config/app.php」の「Datasources – default」をコピーして他DB接続用の「common(任意の名前)」を作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php return [ 'Datasources' => [ 'default' => [ // ・・・ ], 'common' => [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Mysql', 'persistent' => false, 'host' => 'localhost', //'port' => 'non_standard_port_number', 'username' => 'root', 'password' => 'secret', 'database' => 'common', 'encoding' => 'utf8', 'timezone' => 'Asia/Tokyo', 'flags' => [], 'cacheMetadata' => true, 'log' => false, 'quoteIdentifiers' => false, 'url' => env('DATABASE_URL', null), ], ], ]; |
◯ Table定義にdefaultConnectionNameを追記
1 2 3 4 5 6 7 8 9 | class UsersTable extends Table { // DB切替(default -> common) public static function defaultConnectionName() { return 'common'; } } |