我可以创建PDO
的实例并将其成功注入。我直接定义了PDO::class
并将其注入到__construct(PDO $pdo)
的构造函数中。我需要像PDO1::class
和PDO2::class
这样的东西来注入它,如下所示:__construct(PDO1 $pdo1, PDO2 $pdo2)
,但这显然行不通。只有一个PDO
类,我需要做的是使用不同数据库凭据的2个实例。
最好的方法是什么?
我像这样通过PDO设置了一个数据库定义,它的工作原理是:
文件:dependencies.php
use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;
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;
}
}
我在this文章中看到了类似的内容:
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);
},
]);
};
但这是最好的方法吗?以及如何在无需注入整个容器的情况下访问存储库类中的连接?
答案 0 :(得分:3)
如果您的应用中有一个类的多个实例(这里有PDO
类的多个实例),则必须配置每次要插入哪个实例。
这意味着PDO
无法自动连接,因为PHP-DI无法根据服务/控制器/等来确定所需的实例。
您需要使用配置(请参见http://php-di.org/doc/php-definitions.html#autowired-objects)来定义为每个服务注入哪个实例(示例中为db1
或db2
)。
return [
MyService::class => DI\autowire()
->constructorParameter('pdo', DI\get('db1'))
->constructorParameter('pdo2', DI\get('db2')),
'db1' => function (ContainerInterface $c) {
return new PDO();
},
'db2' => function (ContainerInterface $c) {
return new PDO();
},
];
答案 1 :(得分:2)
您有多种选择:
1。连接代理
示例:
use PDO;
class ConnectionProxy
{
private $pdo;
private $pdo2;
public function __construct(PDO $pdo, PDO $pdo2)
{
$this->pdo = $pdo;
$this->pdo2 = $pdo2;
}
public function getPdo(): PDO
{
return $this->pdo;
}
public function getPdo2(): PDO
{
return $this->pdo2;
}
}
容器定义:
return [
ConnectionProxy::class => function (ContainerInterface $c) {
return new ConnectionProxy(
$c->get('db1'),
$c->get('db2')
);
},
'db1' => function (ContainerInterface $c) {
return new PDO();
},
'db2' => function (ContainerInterface $c) {
return new PDO();
},
];
用法
class MyService
{
private $pdo;
private $pdo2;
public function __construct(ConnectionProxy $connectionProxy)
{
$this->pdo = $connectionProxy->getPdo();
$this->pdo2 = $connectionProxy->getPdo2();
}
}
2。扩展PDO
class PDO1 extends PDO
{
}
class PDO2 extends PDO
{
}
容器定义:
return [
PDO1::class => function (ContainerInterface $c) {
return new PDO();
},
PDO2::class => function (ContainerInterface $c) {
return new PDO();
},
];
用法
class MyService
{
private $pdo;
private $pdo2;
public function __construct(PDO1 $pdo, PDO2 $pdo2)
{
$this->pdo = $pdo;
$this->pdo2 = $pdo2;
}
}
3。自动连线的对象
请参阅Matthieu Napoli的答案:https://stackoverflow.com/a/57758106/1461181