之前我问过using multiple entity manager for FOSUserBundle,事实证明FOSUserBundle已经(部分)支持了这一点。我需要做的就是在model_manager_name
参数中指定我想要使用的连接/管理器,如here所述
fos_user:
# ........
model_manager_name: account1
示例app / config / config.yml
使用此FOSUserBundle将使用account1
连接,并在该连接的数据库中使用用户信息。
doctrine:
dbal:
default_connection: default
connections:
account2:
dbname: account2
user: account2
password: password2
driver: pdo_mysql
host: localhost
port: ~
charset: UTF8
account1:
dbname: account1
user: account1
password: password1
driver: pdo_mysql
host: localhost
port: ~
charset: UTF8
default:
dbname: account
user: account
password: password
driver: pdo_mysql
host: localhost
port: ~
charset: UTF8
我的应用要求当用户转到(例如)http://myapp.com/a/account1时,该应用将使用account1
连接,转到http://myapp.com/a/account2将使用account2
&# 39;连接。对于我的应用程序的逻辑,这很容易从我的控制器完成,因为我可以使用类似下面的内容;
$em = $this->get('doctrine')->getManager('account2');
$repository = $this->get('doctrine')->getRepository($class, 'account2')
对于登录部分,它并不那么容易。 FOSUserBundle作为服务容器运行,我不知道在哪里/如何动态更改model_manager_name
的值。我知道虽然在FOS\UserBundle\DependencyInjection\FOSUserExtension
我可以通过以下方式手动更改其值;
class FOSUserExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$processor = new Processor();
$configuration = new Configuration();
$config = $processor->processConfiguration($configuration, $configs);
$config['model_manager_name'] = 'account2';
// .................
有什么想法?
答案 0 :(得分:5)
配置存储在容器内,确切地说是fos_user.model_manager_name
。
您可以编写编译器传递。这将在冻结容器之前执行,它是您可以更改容器的最后一个位置,它是根据其他服务更改容器的位置。
您的编译器传递将如下所示:
// src/Acme/DemoBundle/DependencyInjection/Compiler/ChangeModelManagerPass.php
namespace Acme\DemoBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class ChangeModelManagerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$request = $container->get('request');
$uri = $request->getUri();
// check if the uri matches some pattern which will cause a change in the
// `model_manager_name` setting
if (...) {
// ... do some stuff to get the correct model manager name
// set the setting
$container->setParameter('fos_user.model_manager_name', ...);
}
}
}
阅读Richard Miller在the docs或this great blog post中有关编译器传递的更多信息。