【发布时间】:2013-03-06 07:15:57
【问题描述】:
我之前询问过using multiple entity manager for FOSUserBundle,结果发现 FOSUserBundle (部分)支持它。我需要做的就是在model_manager_name 参数中指定我要使用的连接/管理器,如here 解释的那样
fos_user:
# ........
model_manager_name: account1
示例应用程序/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 的连接。对于我的应用程序的逻辑,这很容易从我的控制器中完成,因为我可以使用以下内容;
$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';
// .................
有什么想法吗?
【问题讨论】:
标签: symfony dependency-injection fosuserbundle