CakePHP Create Datasource on the fly

Want to switch between different databases in a CakePHP action? Do you want to use different database for different clients or sites? Don’t worry the solution more simpler than you thought it to be.

Following this link and few other similar links this is how I connected to different datasource in CakePHP.

[php]try {
ConnectionManager::create(‘default’, $this->config);
$db = ConnectionManager::getDataSource(‘default’);
} catch (MissingDatabaseException $e) {

Where $this->config contains the following set of data:

‘password’ => ‘*****’,
‘login’ => ‘dbusername’,
‘host’ => ‘dbhostname’,
‘database’ => ‘dbname’,
‘datasource’ => ‘Database/Mysql’

Leave a Reply