问题
I have a raw sql query I need to run, but the database name changes in each environment (live: db, dev db_test)
I need to get the current database name from the databases.yml file.
How can I get just the current database name?
I am using the Propel ORM
回答1:
Initially I thought this would be pretty easy via sfPropelDatabase::getConfiguration()
but that returns an array. As such, I had to parse the result to get the data, and I think there's probably a better way than this:
$propel_config = sfPropelDatabase::getConfiguration();
preg_match('/dbname=([^;]+);/', $propel_config['propel']['datasources']['propel']['connection']['dsn'], $matches);
echo $matches[1];
Anyone got anything better?
回答2:
The following code works in Propel2 -- essentially the same as the accepted answer.
$mgr = \Propel\Runtime\Propel::getConnectionManager($connectionId);
$dsn = $mgr->getConfiguration()['dsn'];
preg_match('/dbname=([^;]+)/', $dsn, $matches);
echo $matches[1];
来源:https://stackoverflow.com/questions/2837659/in-symfony-1-2-how-can-i-get-the-current-database-name-from-the-database-yml-fil