Easy peasy database config

By Joel Moss (joelmoss)
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
Download code 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();
    }
}

 

Comments 160

CakePHP Team Comments Author Comments
 

Comment

1 Why didnt I think of that .

Pretty cool, but ... that's what svn:ignore is for :p
Posted Jan 17, 2007 by Yuka Poppe
 

Comment

2 Just what I needed...

Thanks, I needed an easy way for doing this. Instead of testing using the SERVER_ADDR value, I'm probing the file system for the existence of a file that only resides on the DEV server (outside my app tree so I don't go uploading it accidentally). Within this file, I have DEV-specifig configs.

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.
Posted May 26, 2008 by Brian Warren
 

Comment

3 Or maybe

Or maybe match against the http_host like this:

stristr($_SERVER['HTTP_HOST'],$live_server_url) === false
Where the $live_server_url is, duh, the live server url :)
Posted May 30, 2008 by Andrey Kabakchiev
 

Comment

4 Little addition: cross-server svn friendly config

Well after a little thinking I came up with a solution for server specific configs. This can be used in conjunction with the above, and
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 );
    }
?>
Posted Aug 17, 2009 by Attila Foldesi