Easy peasy database config
Like a lot of developers out there, I use Subversion to keep control of my code and projects, and I also use a different database for development and production. But when using Cake this can be a problem when checking out my code from development to production. Unless I edit my database.php with my production config, the production code would have problems, as it would be trying to access data from the development database.
What I needed was an easy-peasy way of being able to check in my code to production without having to edit the database.php config file. So what I did was very simple and can be found below.
For a full write up of this trick and other Cake stuff, please see my Blog at http://joelmoss.info
What I needed was an easy-peasy way of being able to check in my code to production without having to edit the database.php config file. So what I did was very simple and can be found below.
For a full write up of this trick and other Cake stuff, please see my Blog at http://joelmoss.info
class DATABASE_CONFIG {
var $development = array(
'driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'localhost',
'login' => 'user',
'password' => 'passwd',
'database' => 'app_devel'
);
var $production = array(
'driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'localhost',
'login' => 'user',
'password' => 'passwd',
'database' => 'app'
);
var $test = array(
'driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'localhost',
'login' => 'user',
'password' => 'passwd',
'database' => 'app_test'
);
var $default = array();
function __construct()
{
$this->default = ($_SERVER['SERVER_ADDR'] == '127.0.0.1') ?
$this->development : $this->production;
}
function DATABASE_CONFIG()
{
$this->__construct();
}
}








function __construct()
{
$this->default = (fread(fopen('/etc/hostname', 'r'), 3) == 'dev') ? $this->development : $this->production;
}
this way you can move around your app to different servers, as long as you have the appropriate config file, like:
(file: app/config/localconfigs/localhost.php)
<?php
/**
* SQL config
*/
Configure::write( 'sql_host', 'localhost');
Configure::write( 'sql_user', 'user' );
Configure::write( 'sql_pwd', 'passwd');
Configure::write( 'sql_dbname', 'mydb' );
?>
My database.php looks like this:
<?php
class DATABASE_CONFIG {
var $default = array();
protected $defaultConfigVal = '';
function __construct() {
$this->default = array(
'driver' => $this -> getConfigVal( 'sql_driver', 'mysql'),
'persistent' => $this -> getConfigVal( 'sql_persistent', false ),
'host' => $this -> getConfigVal( 'sql_host' ),
'port' => $this -> getConfigVal( 'sql_port' ),
'login' => $this -> getConfigVal( 'sql_user' ),
'password' => $this -> getConfigVal( 'sql_pwd' ),
'database' => $this -> getConfigVal( 'sql_dbname' ),
'schema' => $this -> getConfigVal( 'sql_schema' ),
'prefix' => $this -> getConfigVal( 'sql_prefix' ),
'encoding' => $this -> getConfigVal( 'sql_encoding' ),
);
}
protected function getConfigVal( $configName, $defaultConfigVal = NULL ) {
if ( ! is_null( $configVal = Configure::read( $configName ) ) ) {
return $configVal;
} else {
return is_null( $defaultConfigVal ) ? $this->defaultConfigVal : $defaultConfigVal;
}
}
}
?>
The trick, as you might have guessed:
make a php file, and require it at (the end of) core.php:
<?php
$configsDir = APP . 'config' . DS . 'localconfigs';
$defaultConfig = $configsDir . DS . 'localhost.php';
$configFile = $configsDir . DS . $_SERVER[ 'HTTP_HOST' ] . '.php';
if ( file_exists( $configFile ) ){
include_once( $configFile );
} else {
include_once( $defaultConfig );
}
?>
stristr($_SERVER['HTTP_HOST'],$live_server_url) === falseWhere the $live_server_url is, duh, the live server url :)
Unfortunately, I'm repeating this "DEV server check" in /app/webroot/index.php and /app/webroot/test.php and /app/config/database.php because I couldn't figure out a way to set it up so that my whole application AND cake console receive my configuration. Not very DRY.
Comments are closed for articles over a year old