【发布时间】:2018-07-27 10:53:05
【问题描述】:
我想从默认的 em 更改为名为 'ps' 的 em。配置是正确的,在控制器中我可以简单地输入$this->getManager('ps')->getConnection('ps');。
但是我想创建一个依赖注入的服务,它也需要访问这个连接。
<?php
namespace AppBundle\Service;
use Doctrine\ORM\EntityManagerInterface;
class HilaService
{
private $entityManager;
private $connection;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
$this->connection = $entityManager->getConnection('ps');
}
public function getCategories(){
$query = $this->connection->query(
'SQL ....'
);
$r = $query->execute();
}
}
由于我无法选择实体管理器“ps”,它也无法加载连接“ps”,这会导致错误:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ps_xxx' doesn't exist
我能以某种方式将参数传递给注入吗?或者注入一些“父对象”然后调用->getManager()?
【问题讨论】:
-
您只需要连接吗?
标签: symfony dependency-injection