Tuesday, September 29, 2009

Multiple Databases, Database Switching in CAKEPHP

This is a small tip but more powerfu l and useful tweak for the developers who will often change the database configuration details while moving to developement environment to live env and vice versa.

<?php
class DATABASE_CONFIG {
var $development = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '',
'login' => 'your_local_username',
'password' => 'your_local_password',
'database' => 'your_local_database',
'schema' => '',
'prefix' => '',
'encoding' => ''
);
var $production = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'your_remote_server',
'port' => '',
'login' => 'your_remote_username',
'password' => 'your_remote_password',
'database' => 'your_remote_database',
'schema' => '',
'prefix' => '',
'encoding' => ''
);
var $default = array();
function __construct(){
$this->default = ($_SERVER['SERVER_ADDR'] == '127.0.0.1') ? $this->development : $this->production;
}
function DATABASE_CONFIG(){
$this-> __construct();
}
}
?>


By the above script you can automatically switch your datbases according to the server.
And one more useful tip
we often used to change the value of debug constant in core.php file Right.... ?
Below is the small tip for that. By this, debug will be set to zero automatically
in live environment and it will be set to '2' in the development environment.
In app/core.php replace with following code :

FIND:

Configure::write('debug',2);

REPLACE WITH :

if($_SERVER['SERVER_ADDR'] == '127.0.0.1'){
Configure::write('debug', 2);
}else{
Configure::write('debug', 0);
}

Labels: , , , ,

posted by adityachandra @ 11:18 PM,


0 Comments:

Post a Comment