LoginSignup
1
1

More than 3 years have passed since last update.

CakePHP3で複数のデータベースに接続する方法

Last updated at Posted at 2020-03-30

config/app.phpに接続情報を明記しましょう。

app.php
'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'user_a', 
        'password' => 'password',
        'database' => 'database_a',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'log' => false,
    ],
    'database_b' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'user_b', 
        'password' => 'password',
        'database' => 'database_b',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'log' => false,
    ],
],

これでおしまい。

別記事でCakePHP3で別々のデータベースのテーブルをjoinする方法を紹介していますので、気になる方はこちらの記事をご覧ください。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1