I've 3 copies of my code - on development workstation, a test server and a production server.
Accordingly, I have 3 separate DB configs in the database.php
file named dev, test & production.
I'm trying to figure out a way to switch the connection based on a value written to the Configure
class. The way I'm trying to accomplish this out is to require a file called instance.php
in the last line of bootstrap.php
.
require( APP . 'Config/instance.php' );
My instance.php
looks like:
<?php
// Configure instance type
Configure::write( 'InstanceType', 'Development' );
//Configure::write( 'InstanceType', 'Test' );
//Configure::write( 'InstanceType', 'Production' );
On the development workstation, the first line will be uncommented. Likewise on Test and Production the 2nd and 3rd lines will remain uncommented.
What I need is a method (perhaps in AppModel
) that'll get executed before any other Model loads and will set the correct DB config. using the following code:
// Set DB Config
switch( Configure::read( 'InstanceType' ) ) {
case 'Development':
//default:
$this->useDbConfig = 'default';
break;
case 'Test':
$this->useDbConfig = 'test';
break;
case 'Production':
$this->useDbConfig = 'production';
break;
}
OR (condensed version)
$this->useDbConfig = Configure::read( 'InstanceType' ) == 'Development' ? 'default' : strtolower( Configure::read( 'InstanceType' ) );
I tried to put this into a __construct()
method inside AppModel
but that started throwing a bunch of errors about missing arguments to __construct
.
The outcome is to have:
- All models utilize the given DB connection.
- Not mess around with the DB specifier individually in each model.
- Not having to maintain 3 different copies of
database.php
on the 3 platforms.
How can I make this work?
Thank you.