Symfony2 - 运行时的动态 Doctrine 数据库连接 [英] Symfony2 - Dynamic Doctrine Database Connections at Runtime

查看:16
本文介绍了Symfony2 - 运行时的动态 Doctrine 数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个很好的解决方案,以利用 Doctrine 进行实体管理,以在 Symfony 中实现数据库的即时连接.

I am looking for a good solution to on-the-fly connection of databases within Symfony utilizing Doctrine for entity management.

我的情况是,我们服务的所有入站用户都将访问 *.website.com 地址,例如 client1.website.com.

The scenario I have is that all inbound users to our service will be visiting *.website.com addresses, like client1.website.com.

我们想为 Client 表使用一个 Doctrine 实体,然后根据他们帐户的 URL 即时查找他们的数据库凭据.

We would like to use one Doctrine entity for the Client table to then look up their database credentials based on the URL of their account on the fly.

到目前为止,我在 stackoverflow 上找到了以下讨论动态更改数据库凭据的主题,但没有明确的可行解决方案.

So far I have found the following topics here on stackoverflow that discuss dynamically changing the database credentials--but no clear workable solutions.

我想提议合作制定一个可行的解决方案,我将为其他希望在 Symfony 中修改数据库连接参数的人整理一篇博客/教程.

I'd like to propose collaborating to put together a working solution, and I'll put together a blog/tutorial post for other folks looking to modify database connection parameters within Symfony.

这里有一些相关的帖子:

Here are some related posts:

动态数据库连接symfony2

Symfony2,动态数据库连接/早期覆盖 Doctrine Service

谢谢!

推荐答案

如果 $em 是现有的实体管理器并且你想重用它的配置,你可以使用这个:

If $em is existing entity manager and you want to reuse it's configuration, you can use this:

$conn = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => '',
    'dbname'   => 'foo'
);

$new = DoctrineORMEntityManager::create(
    $conn,
    $em->getConfiguration(),
    $em->getEventManager()
);

这篇关于Symfony2 - 运行时的动态 Doctrine 数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆