Symfony:多个实体经理(奏鸣曲)

Symfony:多个实体经理(奏鸣曲)

问题描述:

我有一个难题,想要一个answear。我正在与Symfony合作,我安装了Sonata来管理管理区域。当我完成这样做,我的提示行给我这个错误:Symfony:多个实体经理(奏鸣曲)

This is the error

这是我的代码:

parameters: 

services:  
    app.security.user_login_form_authenticator: 
     class: AppBundle\Security\UserLoginFormAuthenticator 
     autowire: true 

    app.security.admin_login_form_authenticator: 
     class: AppBundle\Security\AdminLoginFormAuthenticator 
     autowire: true 

请帮助我。

+0

您是否在项目的配置文件中定义了多个EntityManger? – Forer

+0

我想是的。在我安装了奏鸣曲之后,提示行告诉我图片中的错误https://i.stack.imgur.com/ZrFCL.png –

自动装配功能很方便,但它有一定的局限性。

正如你所说,你有多个实体管理器实例。所以,Symfony不知道应该将哪些注入到您的服务中。如果可以更改服务定义,则可以设置autowiring_types参数来指定依赖项的默认实现。但通常的实体管理器服务由DoctrineBundle定义,您不能直接配置它。 (据我所知,Doctrine配置不提供设置选项。)

因此,最简单的方法是手动指定实体管理器:只需将实体管理器服务标识(doctrine.orm.XXX_entity_manager)传递给你的服务的构造函数参数。

services:  
    app.security.user_login_form_authenticator: 
     class: AppBundle\Security\UserLoginFormAuthenticator 
     arguments: [ '@doctrine.orm.XXX_entity_manager' ] 

    app.security.admin_login_form_authenticator: 
     class: AppBundle\Security\AdminLoginFormAuthenticator 
     arguments: [ '@doctrine.orm.YYY_entity_manager' ] 

显然,如果服务具有其他依赖关系,您还需要指定它们。