0
votes

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

2
Is it possible to use the current database connection, or do you really need the database name to create the connection yourself? If you use "regular" Propel you can use Propel::getConnection() to get a PDO connection, I think you can also do this in Symfony.Jan Fabry

2 Answers

2
votes

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?

0
votes

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];