Skip to content

Instantly share code, notes, and snippets.

@hugodias
Created July 20, 2012 13:58
Show Gist options
  • Save hugodias/3150869 to your computer and use it in GitHub Desktop.
Save hugodias/3150869 to your computer and use it in GitHub Desktop.
CakePHP working with multiple environment databases
<?php
/**
* CakePHP 2.x
* This is a database sample file for use multiple environment in CakePHP
* To find out what the environment is being used at the time just check the configuration 'current_environment'.
* E.g: echo Configure::read('current_environment');
*/
class DATABASE_CONFIG
{
public $default = null;
# Production database settings
public $prod =
array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'production_user',
'password' => 'production_password',
'database' => 'production_database',
'prefix' => '',
'encoding' => 'utf8',
);
# Development database settings
public $dev =
array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'development_user',
'password' => 'development_password',
'database' => 'development_database',
'prefix' => '',
'encoding' => 'utf8',
);
# Tests database settings
public $tests =
array();
function __construct ()
{
# If you do not find the server name, automatically places the environment for developing
if(isset($_SERVER['SERVER_NAME'])){
# Environments with their urls
switch($_SERVER['SERVER_NAME']){
case 'localhost':
$this->default = $this->dev;
# Changing the environment to 'development'
Configure::write('current_environment','dev');
# Sets the Debug to 2
Configure::write('debug',2);
break;
case 'myapp.com':
$this->default = $this->prod;
# Changing the environment to 'production'
Configure::write('current_environment','prod');
# Sets the Debug to 0 ( Production )
Configure::write('debug',0);
break;
}
}
else
{
$this->default = $this->dev;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment