CodeIgniter database configuration + Doctrine CLI
My config/database.php
file contains a simple conditional that swaps out database credentials based on the server it lives:
$server = $_SERVER['SERVER_NAME'];
if(($server=='localhost')||($server=='127.0.0.1'))
{
$db['default']['hostname'] = 'MY_LOCAL_IP';
$db['default']['username'] = 'MY_LOCAL_USERNAME';
$db['default']['password'] = 'MY_LOCAL_PASSWORD';
$db['default']['database'] = 'MY_LOCAL_DB';
}
else
{
$db['default']['hostname'] = 'MY_PROD_IP';
$db['default']['username'] = 'MY_PROD_USERNAME';
$db['default']['password'] = 'MY_PROD_PASSWORD';
$db['default']['database'] = 'MY_PROD_DB';
}
I started playing around with Doctrine today and one thing that caught my eye was the Command Line Interface. The prompt can 开发者_StackOverflowdo all sorts of things, one of which is verification that doctrine is properly configured for production use.
But when calling ensure-production-settings
in the CLI, the interface yells at me stating that SERVER_NAME is an undefined index. While this isn't much of an issue, it got me thinking that Doctrine might be silently complaining of the same thing in the background during the execution of my application.
I guess I could make two separate database.php files - but before that - has anybody experienced issues with Doctrine CLI after making a change to config/database.php
?
Well $_SERVER['SERVER_NAME']
is not set in CLI mode, since this information is provided by the CGI-interface of your webserver (or by the webserver itself when using php as a module).
Why don't you use environments for that?
Without breaking what you did right now, as an execption, you could define a persistent values for CLI since it will not recognized some environment variable...
if(($server=='localhost')||($server=='127.0.0.1'))
{
$db['default']['hostname'] = 'MY_LOCAL_IP';
$db['default']['username'] = 'MY_LOCAL_USERNAME';
$db['default']['password'] = 'MY_LOCAL_PASSWORD';
$db['default']['database'] = 'MY_LOCAL_DB';
}
else
{
$db['default']['hostname'] = 'MY_PROD_IP';
$db['default']['username'] = 'MY_PROD_USERNAME';
$db['default']['password'] = 'MY_PROD_PASSWORD';
$db['default']['database'] = 'MY_PROD_DB';
}
// To catch CLI
if(defined('STDIN'))
{
$db['default']['hostname'] = 'MY_CLI_IP';
$db['default']['username'] = 'MY_CLI_USERNAME';
$db['default']['password'] = 'MY_CLI_PASSWORD';
$db['default']['database'] = 'MY_CLI_DB';
}
精彩评论