Easy Dynamic Database Connection in CakePHP

If you’re like me, you’re strikingly handsome you create Cake applications on your computer, then upload them for testing (and usage) to a server. It’s annoying to have to change the /app/config/database.php file for each new testing environment, especially if the same Cake application is being used on multiple machines.
Thankfully there’s a really simple way around this crap.

Simply replace Cake’s /app/config/database.php file with the following:
<?php
class DATABASE_CONFIG {
#localhost
var $local = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'local',
'prefix' => '');

#dev server
var $dev = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'mysql.dev.com',
'login' => 'dev',
'password' => 'password',
'database' => 'dev',
'prefix' => '');

#live server
var $live = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'mysql.live.com',
'login' => 'live',
'password' => 'password',
'database' => 'live',
'prefix' => '');

#switch between configs
var $default = array();
var $test = array();
function __construct() {

#wildcard the subdomains
$host_r = explode('.', $_SERVER['SERVER_NAME']);
if(count($host_r)>2) while(count($host_r)>2)array_shift($host_r);
$mainhost = implode('.', $host_r);

#switch between servers
switch(strtolower($mainhost)) {
case 'localhost':
$this->default = $this->local;
break;
case 'dev.com':
$this->default = $this->dev;
break;
case 'live.com':
$this->default = $this->live;
break;
default:
$this->default = $this->local;
}
}

#php 4 compatibility
function DATABASE_CONFIG() {
$this->__construct();
}
}
?>
The highlighted code beginning with var $local = array('driver' => 'mysql', corresponds with the case 'localhost': at the bottom.
A Quick Example
So let’s say you have a Cake application running locally on localhost and remotely at mydomain.com, you’re database.php file would look something like this:
<?php
class DATABASE_CONFIG {
#localhost
var $local = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'local',
'prefix' => '');

#live server
var $live = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'mysql.mydomain.com',
'login' => 'live',
'password' => 'password',
'database' => 'live',
'prefix' => '');

#switch between configs
var $default = array();
var $test = array();
function __construct() {

#wildcard the subdomains
$host_r = explode('.', $_SERVER['SERVER_NAME']);
if(count($host_r)>2) while(count($host_r)>2)array_shift($host_r);
$mainhost = implode('.', $host_r);

#switch between servers
switch(strtolower($mainhost)) {
case 'localhost':
$this->default = $this->local;
break;
case 'mydomain.com':
$this->default = $this->live;
break;
default:
$this->default = $this->local;
}
}

#php 4 compatibility
function DATABASE_CONFIG() {
$this->__construct();
}
}
?>
Now when you upload your CakePHP application from your localhost directory to mydomain.com, you won’t have to fiddle with the database.php file. It’ll recognize that Cake’s running on mydomain.com and will adjust the MySQL connection accordingly.
Rock on roll.