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

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

      1. 如何在 slim 4 中设置和注入多个 PDO 数据库连接

        时间:2023-10-04

              <legend id='S7eIw'><style id='S7eIw'><dir id='S7eIw'><q id='S7eIw'></q></dir></style></legend>
                <tbody id='S7eIw'></tbody>

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

                • <small id='S7eIw'></small><noframes id='S7eIw'>

                • 本文介绍了如何在 slim 4 中设置和注入多个 PDO 数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我可以创建一个 PDO 的实例并成功注入它.我直接定义了 PDO::class 并用 __construct(PDO $pdo) 将它注入到构造函数中.我需要像 PDO1::classPDO2::class 之类的东西来注入它,如下所示:__construct(PDO1 $pdo1, PDO2 $pdo2) 但这显然行不通.只有一个 PDO 类,我需要做的是它的 2 个具有不同数据库凭据的实例.
                  最好的方法是什么?

                  I could make an instance of PDO and inject it successfully. I defined the PDO::class directly and injected it in the constructor with __construct(PDO $pdo). I would need something like PDO1::class and PDO2::class to inject it like follows: __construct(PDO1 $pdo1, PDO2 $pdo2) but that obviously doesn't work. There is only one PDO class and what I need to do is 2 instances of it with different database credentials.
                  What is the best way to do it?

                  我像这样通过 PDO 设置了一个数据库定义并且它可以工作:

                  I set up one definition of a database via PDO like this and it works:

                  文件:dependencies.php

                  use DIContainerBuilder;
                  use PsrContainerContainerInterface;
                  
                  return function (ContainerBuilder $containerBuilder) {
                      $containerBuilder->addDefinitions([
                          PDO::class => function (ContainerInterface $c) {
                              $dbSettings = $c->get('settings')['db1'];
                              $dsn = 'mysql:host=' . $dbSettings['host'] . ';dbname=' . $dbSettings['dbname'];
                              $options = [
                                  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                                  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                                  PDO::ATTR_EMULATE_PREPARES => false,
                              ];
                              return new PDO($dsn, $dbSettings['user'], $dbSettings['pass'], $options);
                          },
                      ]);
                  };
                  

                  文件:index.php

                  ...
                  // Set up dependencies
                  $dependencies = require __DIR__ . '/../app/dependencies.php';
                  $dependencies($containerBuilder);
                  // Build PHP-DI Container instance
                  $container = $containerBuilder->build();
                  // Set container to create App with on AppFactory
                  AppFactory::setContainer($container);
                  // Instantiate the app
                  $app = AppFactory::create();
                  ...
                  

                  文件 SomeRepository.php

                  use PDO;
                  
                  class SomeRepository{
                  
                      protected $pdo;
                  
                      public function __construct(PDO $pdo) {
                          $this->pdo = $pdo;
                      }
                  }
                  

                  我在这篇文章中看到过类似的内容:

                  I've seen something like this in this article:

                  return function (ContainerBuilder $containerBuilder) {
                      $containerBuilder->addDefinitions([
                          'db1' => function (ContainerInterface $c) {
                              $db1Settings = $c->get('settings')['db1'];
                              $dsn = 'mysql:host=' . $db1Settings['host'] . ';dbname=' . $db1Settings['dbname'];
                              $options = [ ... ];
                              return new PDO($dsn, $db1Settings['user'], $db1Settings['pass'],$options);
                          },
                          'db2' => function (ContainerInterface $c) {
                              $db2Settings = $c->get('settings')['db2'];
                              $dsn = 'mysql:host=' . $db2Settings['host'] . ';dbname=' . $db2Settings['dbname'];
                              $options = [ ... ];
                              return new PDO($dsn, $db2Settings['user'], $db2Settings['pass'],$options);
                          },
                  
                      ]);
                  };
                  

                  但这是最好的方法吗?以及如何在不必注入整个容器的情况下访问存储库类中的连接?

                  But is it the best way to do it? And how can I access the connections in a repository class without having to inject the whole container?

                  推荐答案

                  您有多种选择:

                  1. 扩展 PDO
                  2. 自动装配的对象

                  1.扩展 PDO

                  use PDO;
                  
                  class PDO2 extends PDO
                  {
                      // must be empty
                  }
                  

                  容器定义:

                  use PDO2;
                  
                  // ...
                  
                  return [
                      PDO::class => function (ContainerInterface $container) {
                          return new PDO(...);
                      },
                  
                      PDO2::class => function (ContainerInterface $container) {
                          return new PDO2(...);
                      },
                  ];
                  

                  使用

                  use PDO;
                  use PDO2;
                  
                  class MyRepository
                  {
                      private $pdo;
                  
                      private $pdo2;
                      
                      public function __construct(PDO $pdo, PDO2 $pdo2)
                      {
                          $this->pdo = $pdo;
                          $this->pdo2 = $pdo2;
                      }
                  }
                  

                  2.自动装配对象

                  参见 Matthieu Napoli 的回答:https://stackoverflow.com/a/57758106/1461181

                  See Matthieu Napoli's answer: https://stackoverflow.com/a/57758106/1461181

                  这篇关于如何在 slim 4 中设置和注入多个 PDO 数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:不能使用整数值多次执行准备好的语句 下一篇:PHP PDO 查询为 FLOAT 字段返回不准确的值

                  相关文章

                  最新文章

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

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

                    2. <legend id='HGnN0'><style id='HGnN0'><dir id='HGnN0'><q id='HGnN0'></q></dir></style></legend>

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

                    3. <tfoot id='HGnN0'></tfoot>