Easy CakePHP deployment: Environment & Database Config
I just digged Rafael Bandeiras Environment class which is supposed to help handling multiple enviroments. It really is a nice piece of code and i don’t find it overkill either.
After adding it to my app i thought how i could use my setups to change the database with ease. I then remembered reading an bakery article from Joel Moss on how one could use the object constructor in the DATABASE_CONFIG class. Well.. both techniques go well together!
Environment writes the currently loaded environment name to the config, so we just use that info to switch the database config too.
class DATABASE_CONFIG {
public $default = array(
'host' => 'localhost',
'driver' => 'mysql',
'persistent' => false,
'login' => 'DEFAULT_USERNAME',
'password' => 'DEFAULT_PASSWORD',
'database' => 'DEFAULT_DATABASE',
);
public $development = array(
'login' => 'dev_username',
'password' => 'dev_password',
'database' => 'dev_db',
);
public $production = array(
'login' => 'live_username',
'password' => 'live_password',
'database' => 'live_db',
);
function __construct() { // the magic
$environment = Configure::read('Environment.name');
if (($environment) && isset($this->{$environment})) {
$this->default = array_merge($this->default, $this->{$environment});
}
}
}
Okay.. the above assumes we have setup two environments called “production” and “development”. What an environment does for you is best explained on the original post. I will show you a example setup that’s in the bootstrap.php.
Environment::configure(
'development',
array('server' => 'localhost'),
array(
'debug' => 2,
'security' => 'low'
)
);
Environment::configure(
'production',
true,
array(
'debug' => 0,
'security' => 'high'
)
);
Now whenever the environment changes the custom database config array by the same name is merged with DATABASE_CONFIG::$default.


Hey, nice to see it getting on someone else’s setup.
Anyway, good approach, wise merge of both ideas!
Why didn’t you post me a pingback?
Hi Rafael – i tried to ping, but i couldn’t find the trackback url on your page. I’ve added your posts url as trackback, but i guess Wordpress didnt know how to communicate.
Hey thanks for the information!
I’ve read the post from rafaelbandeira3, Chris Hartjes, and Neil Crookes and find it very interesting…
However, I’m very new to the different environments concept… Can you explain me like what would be the workflow in order to implement this? I mean, this is what I have in mind:
1 – A developer checks-out/clone the app.
2 – He/she will configure the “config” files to meet their environment (“dev”).
3 – He/she will only “check-in/push” features specific commits.
4 – Finally the SCM will not make updates to their environments configuration files…
Right? I’m sure I’m missing steps… I would appreciate if you can help me on this!
Thanks in advance!
If you have setup the different stages you can store the live, test and dev database settings within the source (SCM-Server). The app itself then can run on three (or more) different locations without the need to config after deployment.
As for local devlopment: I just agree on the first default-config with everyone so we can leave the file alone.
Without this, you would have to manually edit the database.php (or database.php.sample) on each server after you deployed the app (which can be annoying if you ever change the password or add methods, properties, datasources, etc..). The above simply allows you to keep the database.php in your SCM and push updates without all the hassle.
Now to your question specificly:
No, there would still be conflicts after a developer clones the app AND changes the file (it’s in the SCM after all, whatever it is). The changes would affect everyone.
Best is to tell everybody how to name and password-protect their local database. That shouldn’t be an issue and in most prefab XAMP stacks it’s “root:root” anyway.
The trick explained here is aimed at server deployment. Local dev still requires a “agreement” between the people involved.
Thanks, very helpful!