使用Symfony 4,我们正在构建一个多客户应用程序其中每个客户都有一个专用数据库。客户数据库是在创建新客户时自动按需创建的。在此过程中,我们创建了唯一的额外.env文件(特定于客户(,该文件保存数据库连接凭据。
我们有2个数据库连接&2x实体经理-common
&CCD_ 2。customer
实体管理器实际上是默认的实体管理器。显然,customer
实体管理器一开始没有正确的连接参数。我们根据内核请求相应地加载额外的.env文件(例如,基于域(,并相应地设置customer
连接和实体管理器:
$connection = $this->entityManager->getConnection();
$connection->close();
$reflectionConn = new ReflectionObject($connection);
$reflectionParams = $reflectionConn->getProperty('params');
$reflectionParams->setAccessible(true);
$params = $reflectionParams->getValue($connection);
$params['dbname'] = $database; // read from the customer specific .env
$params['user'] = $username; // read from the customer specific .env
$params['password'] = $password; // read from the customer specific .env
$reflectionParams->setValue($connection, $params);
$reflectionParams->setAccessible(false);
$this->entityManager = $this->entityManager->create(
$this->entityManager->getConnection(),
$this->entityManager->getConfiguration()
);
这很好用!
问题是安全上下文(?(-我们使用的FosUserBundle无法授权用户。我猜上面的代码不足以影响安全上下文。
使用标准FosUserBundle实现动态数据库连接+用户授权的目标的正确方法是什么?
问题根本与安全上下文无关。问题(愚蠢的(是,我的内核请求侦听器具有默认优先级(0(,这使得整个客户配置加载和连接设置在登录侦听器发生后进行。
将其设置为512(应该足够(,以便在SessionListener 之前启动