我正在尝试创建一个使用多个数据库的REST API。我需要从值来自令牌连接到数据库。我无法在config.yml和parameters.yml中定义所有数据库,因为可能会添加新数据库。我希望它是完全动态的。我尝试了以下类似的方法。
$connection = $this->container->get('doctrine.dbal.default_connection');
$refConn = new \ReflectionObject($connection);
$refParams = $refConn->getProperty('_params');
$refParams->setAccessible('public');
$params = $refParams->getValue($connection);
$params['dbname'] = $domain;
$refParams->setAccessible('private');
$refParams->setValue($connection, $params);
$this->container->get('doctrine')->resetManager('default');
$em = $this->getDoctrine()->getManager('default');
当我“转储($ em);”时它显示了我要连接的数据库,但是当我执行SQL查询时,它将返回默认数据库的数据。
有人做过这样的事情吗?我将不胜感激。
这是完整的代码段。我尚未设置令牌环境,但现在我从标头发送它。
public function getUploadsAction ($Request request)
{
$ domain = $ request -> headers -> get ('domain');
$ connection = $ this-> container-> get ('doctrine.dbal.default_connection');
$ refConn = new \ ReflectionObject ($connection);
$ refParams = $ refConn-> getProperty ('_ params');
$ RefParams-> setAccessible ('public');
$ params = $ refParams-> getValue ($ connection);
$ params ['dbname'] = $ domain;
$ refParams-> setAccessible ('private');
$ refParams-> setValue ($ connection, $ params);
$ $this->container->get ('doctrine') -> resetManager ('default');
$ em = $ this-> getDoctrine () -> getManager ('default');
$ sql = 'SELECT * upload WHERE RECNO ='. $ request-> headers-> get ('recno');
$ statement = $ em-> getConnection () -> prepare ($ sql);
$ statement->execute();
$ result = $statement->fetchAll();
return $result;
}