【问题标题】:Symfony 3.3 - Entity Manager injection into services with multiple database?Symfony 3.3 - 实体管理器注入具有多个数据库的服务?
【发布时间】: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”参数默认注入实体管理器吗?

感谢您的帮助!

皮埃尔

【问题讨论】:

标签: symfony-3.3


【解决方案1】:

如果您有很多经理,则无法使用 EntityManagerInterface 自动装配。您必须在服务定义中为每项服务选择合适的经理。

RouterBundle\Utils:
    $em: '@your_entity_manager_service'

PS:您应该考虑升级到 sf3.4,因为 3.3 不再维护。它还带有本地绑定,这对于您想要做的事情非常有用。

见: https://symfony.com/blog/new-in-symfony-3-4-local-service-binding

【讨论】:

  • 嗨 Fabien,非常感谢。我快到了,但我收到此错误:服务“RouterBundle\Utils\RoutersUtils”依赖于不存在的服务“doctrine.orm.entity_manager” 。路由器”。您的解决方案仅适用于 3.4 还是适用于 3.3?
  • (即使我尝试教义.orm.entity_managers.router 或教义.orm.default_entity_manager )
  • 它适用于 $em: '@doctrine.orm.entity_manager' 我不明白为什么!!
  • PS:你是对的,3.4 有默认参数注入。在我的情况下它会非常有用......
  • 其实我没有测试服务名。如果您使用许多管理器,您可以使用debug:container 命令找到他们的服务名称
猜你喜欢
  • 2018-05-06
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 2018-02-01
相关资源
最近更新 更多