Last active
June 18, 2018 05:06
-
-
Save chrisvogt/5675126 to your computer and use it in GitHub Desktop.
Example showing how to connect CakePHP database config to AppFog's VCAP_SERVICES environment array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DATABASE_CONFIG { | |
public $default = array(); | |
function __construct() { | |
## --- APPFOG --- ## | |
// Read in the VCAP_SERVICES environment variable, parse the json, and | |
// check to see if it exists. If it does, use the settings for our default | |
// database connection. | |
$vcap_services = json_decode(getenv("VCAP_SERVICES"), true); | |
if(!empty($vcap_services)) { // database connection credentials | |
$this->default = array( | |
'datasource' => 'Database/Mysql', | |
'persistent' => false, | |
'host' => $vcap_services['mysql-5.1'][0]['credentials']['host'], | |
'login' => $vcap_services['mysql-5.1'][0]['credentials']['username'], | |
'password' => $vcap_services['mysql-5.1'][0]['credentials']['password'], | |
'database' => $vcap_services['mysql-5.1'][0]['credentials']['name'], | |
'prefix' => '', | |
'encoding' => 'utf8', | |
); | |
if ($vcap_services['mysql-5.1'][0]['credentials']['port']) { | |
$this->default['port'] = $vcap_services['mysql-5.1'][0]['credentials']['port']; // mySQL port | |
} | |
} else { | |
$this->default = array( | |
'datasource' => 'Database/Mysql', | |
'persistent' => false, | |
'host' => 'localhost', | |
'login' => 'user', | |
'password' => 'password', | |
'database' => 'production_database_name', | |
'prefix' => '', | |
// 'encoding' => 'utf8', | |
); | |
} | |
## --- APPFOG --- ## | |
} | |
public $test = array( | |
'datasource' => 'Database/Mysql', | |
'persistent' => false, | |
'host' => 'localhost', | |
'login' => 'user', | |
'password' => 'password', | |
'database' => 'test_database_name', | |
'prefix' => '', | |
//'encoding' => 'utf8', | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment