Symfony 3 connection to multiple databases(Symfony 3 连接到多个数据库)
问题描述
dynamic:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database name%
user: %database_user%
password: %database_password%
charset: UTF8
大家好
我想动态连接到多个数据库.我阅读了本教程 http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html,它的工作原理和解释一样.
I would like to connect to multiple databases dynamically. I read this tutorial http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html and it works as it was explained.
但我唯一的问题是直接从我的控制器更改 dbname 的值以连接到我的所有数据库.
But my only problem is to change the value of dbname directly from my Controller to connect to all my databases.
我会在 24 小时内解决这个问题.如果你有想法我真的需要它
It will be 24 hours that I am on this problem. if you have ideas I really need it
推荐答案
您的代码示例仅显示一个连接.您必须定义数据库连接,例如 文档中的示例:
Your code example shows only one connection. You've to define to database connection, like the example from the documentation:
如果您想要一个简单的方法,请不要使用 EntityManager.如果您只想获取有关数据库的一些信息并且不想存储信息或使用实体,这可能是矫枉过正.然后,只需创建一个自定义连接并使用vanilla"SQL:
If you want an easy way out, don't use an EntityManager at all. It might be overkill if you only want to get some information about the database and don't want to store information or use Entities. Then, just create a custom connection and use 'vanilla' SQL:
$this->get('doctrine.dbal.connection_factory')->createConnection($params);
您的问题并未说明您期望有多少数据库.所以让我们描述两种可能性:少量(固定)数据库或动态连接.
Your question does not say how many database you expect. So let's describe two possibilities: a small number of (fixed) database or a dynamic connection.
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
model_a:
driver: pdo_mysql
host: '%database_hosta%'
port: '%database_porta%'
dbname: '%database_namea%'
user: '%database_usera%'
password: '%database_passworda%'
charset: UTF8
model_b:
driver: pdo_mysql
host: '%database_hostb%'
port: '%database_portb%'
dbname: '%database_nameb%'
user: '%database_userb%'
password: '%database_passwordb%'
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
model_a:
connection: model_a
model_b:
connection: model_b
默认情况下,将使用连接 default.在您的控制器中,您可以切换到另一个连接:
By default, connection default will be used. In your controller, you can switch to another connection:
$customerEm = $this->getDoctrine()->getManager('model_a');
当然,您可以动态选择您的经理:
And of course, you can dynamically choose your Manager:
//just an example, I don't know how you choose which database to use.
$managerName = $company->getModelName();
$em = $this->getDoctrine()->getManager($managerName);
动态创建EntityManager
DoctrineBundle 不支持动态值(除了第一个示例中的配置值).你可以创建你自己的EntityManager, 如果您不想(或不能)定义多个连接,因为动态表名.
Dynamically create EntityManager
The DoctrineBundle has no support for dynamic values (other than configured values like the first example). You can create your own EntityManager, if you don't want (or can't) define multiple connections because the dynamic table name.
创建工厂并将其定义为服务:
Create a factory and define it a service:
class EntityManagerFactory
{
public function createManager($dbname)
{
$isDevMode = false;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);
// or if you prefer yaml or XML
//$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
//$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode);
$conn = [
'driver' => 'pdo_mysql',
'username' => 'foo',
'password' => 'foo',
'dbname' => $dbname
];
// obtaining the entity manager
return EntityManager::create($conn, $config);
}
}
并在您的控制器中使用它:
And use it in your controller:
public function controllerAction()
{
$em = $this->get(EntityManagerFactory::class)->getManager('model-name');
}
这篇关于Symfony 3 连接到多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Symfony 3 连接到多个数据库
基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
