Different cakephp datasource for local and live

534 views Asked by At

My MySQL connection details are different for both my local connection and my deployed live hosted. I am using CakePHP 3

At the moment I have to keep changing the default datasource which is not really the best way to do it.

I have not added two datasources but I am not sure how to switch between them?

'Datasources' => [
    'development' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => '127.0.0.1',
        'port' => '8889',
        'username' => 'root',
        'password' => 'root',
        'database' => 'local',
    ],
    'deployment' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'username',
        'password' => 'password',
        'database' => 'live_database',
    ],
1

There are 1 answers

0
Manuel On BEST ANSWER

In boostrap or in App Controller, paste this

if(Configure::read('debug')){ ConnectionManager::config('deployment'); }

this change the default config of database when the debug is true.