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

      symfony2 动态数据库连接使用学说

      时间:2024-08-15
      • <tfoot id='VFV0C'></tfoot>

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

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

              <tbody id='VFV0C'></tbody>
              <bdo id='VFV0C'></bdo><ul id='VFV0C'></ul>
              1. 本文介绍了symfony2 动态数据库连接使用学说的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我试图在 Symfony 2 中使用教义建立多个数据库连接,但无法做到.

                I am trying to have multiple database connection in Symfony 2 with doctrine but not able to do so.

                我在 Google 和 stack-overflow 中进行了广泛的搜索,但到处都是通过 config.yml 文件或动态数据库完成的,其中所有 DB 都具有相同的 schema/entities .

                I have searched extensively in Google and stack-overflow but everywhere it's done via config.yml file or dynamic database where all DB's have same schema/entities .

                但就我而言,数据库是根据子域确定的,并且所有子域的数据库架构都不相同.

                例如:
                test1.example.com => 应该加载 test1 db
                test2.example.com => 将加载 test2 db

                Ex:
                test1.example.com => Should load test1 db
                test2.example.com => Will load test2 db

                test1 和 test2 DB 都是不同的,都是在 DB 级别创建的,并且在学说中没有实体条目.

                谁能帮我在 Symfony 2 中做到这一点.

                Can anyone please help me how to do this in Symfony 2.

                推荐答案

                在我看来,使用 Doctrines ODM 并不是解决这个问题的正确方法.你仍然可以使用 Doctrine 连接到数据库并查询它们.但是如果你没有实体类,那么使用实体管理器似乎是不合适的.

                It seems to me that using Doctrines ODM is not the right way to approach this. You can still use Doctrine to connect to databases and query them. But if you have no entity classes the use of an entity manager seems to be inappropriate.

                以下是您如何使用教义 Connection 类创建与数据库的连接:

                Here is how you create a connection to a Database with the doctrine Connection class:

                /** @var DoctrineBundleDoctrineBundleConnectionFactory $connectionFactory */
                $connectionFactory = $this->getContainer()->get('doctrine.dbal.connection_factory');
                $connection = $connectionFactory->createConnection(
                    array('pdo' => new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password))
                );
                

                现在您可以将 $connection 用作简单的 PDO 对象:

                Now you can use $connection as a simple PDO object:

                $connection->executeQuery('SELECT * FROM your_table');
                

                您可以将此代码添加为服务,使其随处可访问.
                如果您想为不同的域连接到不同的数据库,您可以使用此代码来识别域:

                You could add this code as a service to make it accessible everywhere.
                If you want to connect to a different database for a different domain you can use this code to identify the domain:

                $this->getRequest()->getHost();
                

                要在 action 中访问域,请执行以下操作:

                To access the domain in an action do this:

                public function yourAction(Request $request, /* ... */)
                {
                    // the Controller extends the Container. So need to get it here:
                    $connectionFactory = $this->get('doctrine.dbal.connection_factory');
                
                    // also access the domain like this:
                    $domain = $request->getHost();
                }
                

                这篇关于symfony2 动态数据库连接使用学说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用 Symfony 测试数据库插入 下一篇:Doctrine_Core::getTable()->findAll() 如何指定顺序?

                相关文章

              2. <legend id='CdN5o'><style id='CdN5o'><dir id='CdN5o'><q id='CdN5o'></q></dir></style></legend>
              3. <tfoot id='CdN5o'></tfoot>
                • <bdo id='CdN5o'></bdo><ul id='CdN5o'></ul>

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

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