<legend id='uY30m'><style id='uY30m'><dir id='uY30m'><q id='uY30m'></q></dir></style></legend>
  • <i id='uY30m'><tr id='uY30m'><dt id='uY30m'><q id='uY30m'><span id='uY30m'><b id='uY30m'><form id='uY30m'><ins id='uY30m'></ins><ul id='uY30m'></ul><sub id='uY30m'></sub></form><legend id='uY30m'></legend><bdo id='uY30m'><pre id='uY30m'><center id='uY30m'></center></pre></bdo></b><th id='uY30m'></th></span></q></dt></tr></i><div id='uY30m'><tfoot id='uY30m'></tfoot><dl id='uY30m'><fieldset id='uY30m'></fieldset></dl></div>

      <bdo id='uY30m'></bdo><ul id='uY30m'></ul>

    <small id='uY30m'></small><noframes id='uY30m'>

      <tfoot id='uY30m'></tfoot>

        Symfony 3 连接到多个数据库

        时间:2024-08-09

            • <i id='qcpMk'><tr id='qcpMk'><dt id='qcpMk'><q id='qcpMk'><span id='qcpMk'><b id='qcpMk'><form id='qcpMk'><ins id='qcpMk'></ins><ul id='qcpMk'></ul><sub id='qcpMk'></sub></form><legend id='qcpMk'></legend><bdo id='qcpMk'><pre id='qcpMk'><center id='qcpMk'></center></pre></bdo></b><th id='qcpMk'></th></span></q></dt></tr></i><div id='qcpMk'><tfoot id='qcpMk'></tfoot><dl id='qcpMk'><fieldset id='qcpMk'></fieldset></dl></div>

              <tfoot id='qcpMk'></tfoot>

              1. <legend id='qcpMk'><style id='qcpMk'><dir id='qcpMk'><q id='qcpMk'></q></dir></style></legend>

                  <tbody id='qcpMk'></tbody>

                <small id='qcpMk'></small><noframes id='qcpMk'>

                  <bdo id='qcpMk'></bdo><ul id='qcpMk'></ul>

                  本文介绍了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 连接到多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:学说2中的问题理解关系映射 下一篇:教义2:在一对多的双向关系中,如何从反面保存?

                  相关文章

                • <tfoot id='9F3Tl'></tfoot>
                • <i id='9F3Tl'><tr id='9F3Tl'><dt id='9F3Tl'><q id='9F3Tl'><span id='9F3Tl'><b id='9F3Tl'><form id='9F3Tl'><ins id='9F3Tl'></ins><ul id='9F3Tl'></ul><sub id='9F3Tl'></sub></form><legend id='9F3Tl'></legend><bdo id='9F3Tl'><pre id='9F3Tl'><center id='9F3Tl'></center></pre></bdo></b><th id='9F3Tl'></th></span></q></dt></tr></i><div id='9F3Tl'><tfoot id='9F3Tl'></tfoot><dl id='9F3Tl'><fieldset id='9F3Tl'></fieldset></dl></div>
                    <bdo id='9F3Tl'></bdo><ul id='9F3Tl'></ul>
                    <legend id='9F3Tl'><style id='9F3Tl'><dir id='9F3Tl'><q id='9F3Tl'></q></dir></style></legend>
                    1. <small id='9F3Tl'></small><noframes id='9F3Tl'>