In Symfony 1.2 how can I get the current database name from the database.yml file in Propel?

我只是一个虾纸丫 提交于 2019-12-12 04:34:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!