Advanced Database Switching in cakePHP

I discovered someone’s code for a DATABASE_CONFIG class slightly to allow for a more robust database switcher. I then altered that code, ever so slightly. Now my database configuration file is a little more concise and elegantly changes databases according to your host.

One word of warning, once you’ve altered your database configuration using the following code, you will no longer be able to use bake.php in the cake/scripts folder. Bake does not have a host, because it is the command line, so it uses the default database, which would, in this case, be empty.

Paste the following into app/config/database.php (between the php tags, of course).

class DATABASE_CONFIG {
		var $walker = array(
						'driver' => 'mysql',
						'connect' => 'mysql_connect',
						'host' => 'localhost',
						'login' => 'username',
						'password' => 'password',
						'database' => 'database_name'
		);
		var $jon = array(
						'driver' => 'mysql',
						'connect' => 'mysql_connect',
						'host' => 'localhost',
						'login' => 'loginname',
						'password' => 'password',
						'database' => 'database_name'
		);
		var $test = array(
						'driver' => 'mysql',
						'connect' => 'mysql_connect',
						'host' => 'localhost',
						'login' => 'user',
						'password' => 'password',
						'database' => 'db_name',
						'prefix' => 'test_'
		);
		var $production = array(
						'driver' => 'mysql',
						'connect' => 'mysql_connect',
						'host' => 'localhost',
						'login' => 'loginname',
						'password' => 'password',
						'database' => 'production_db_name'
		);
		var $default = array();

		function __construct()
		{
			$host_r = explode('.', $_SERVER['SERVER_NAME']);

			//I'm using auto-subdomains, so I want to make sure we're just on the domain here
			if(count($host_r)>2)
				while(count($host_r)>2)array_shift($host_r);

			$mainhost = implode('.', $host_r);
			
			switch(strtolower($mainhost))
			{
				//jon and I have our /etc/hosts edited for development domains
				case 'walker.dev':
					$this->default = $this->walker;
					break;
				case 'jon.dev':
					$this->default = $this->jon;
					break;
				case 'domain.net':
					$this->default = $this->test;
					break;
				default:
					$this->default = $this->production;
			}
		}
		
		//php 4 compatibility
		function DATABASE_CONFIG()
		{
			$this->__construct();
		}
}

Again, if anyone knows the original source of this idea, feel free to comment or contact me and I will amend this entry.

Amended: sdevore (the grumpy old fart ;) says he was the first to add this trick to the wiki.

Also, something I noticed I forgot to mention: In the construct function, you will notice that I commented a line that says I’m using automatic sub-domains. The original version of this did strpos() functions with “.” to get the strings that make up the domain. I thought that this explode/shift/implode was a safer method of finding the top domain when I know there will always be two or more dots in the host string.

 
Blog comments powered by Disqus