【发布时间】:2018-08-18 03:40:46
【问题描述】:
我最近在我的 Symfony 3.3 应用程序中添加了一个新数据库。 然后我的注入实体管理器的服务不再工作并返回以下错误:
无法自动装配服务“RouterBundle\Utils\RoutersUtils”:参数 方法“__construct()”的“$em”引用类 “Doctrine\ORM\EntityManager”,但不存在这样的服务。尝试改变 对其父母之一的类型提示:接口 “Doctrine\ORM\EntityManagerInterface”,或接口 “学说\通用\持久性\对象管理器”。
所以我的 config.yml 文件现在是这样的:
# Doctrine Configuration
doctrine:
dbal:
default_connection: router
connections:
router:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_router_name%'
user: '%database_router_user%'
password: '%database_router_password%'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
row_format: DYNAMIC
app:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_app_name%'
user: '%database_app_user%'
password: '%database_app_password%'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
orm:
default_entity_manager: router
entity_managers:
router:
connection: router
mappings:
RouterBundle: ~
dql:
string_functions:
# Match agains should have the path to the Sha1 class created in the previous step
SHA1: RouterBundle\Extensions\Doctrine\Sha1
app:
connection: app
mappings:
AppBundle: ~
我的一项服务,例如:
<?php
namespace RouterBundle\Utils;
use Doctrine\ORM\EntityManager;
class RoutersUtils
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
...
和我的 services.yml(我尝试添加此参数但不起作用)
...
RouterBundle\Utils:
public: true
arguments:
- '@doctrine.dbal.router_connection'
...
知道如何注入正确的实体管理器吗? 我还可以使用配置文件中的“default_entity_manager: router”参数默认注入实体管理器吗?
感谢您的帮助!
皮埃尔
【问题讨论】:
-
也许注入ManagerRegistry? matthiasnoback.nl/2014/05/…
标签: symfony-3.3