Symfony 3 - 具有多个数据库连接的EntityManager依赖注入

Pre*_*cks 3 dependency-injection entitymanager symfony symfony-3.2

我已经使用guard设置了一个Custom Authenticator并自动连接了该服务.这经过测试,只需配置MySQL即可正常工作.

我现在已经指定了第二个数据库连接(oracle),但Symfony现在不允许在我的服务配置中进行自动装配,因为它不知道在将EntityManager注入自定义Authenticator类时要使用哪个数据库连接.

知道如何配置依赖注入以使用特定的数据库连接,以便我可以继续使用AutoWire.

Unable to autowire argument of type "Doctrine\ORM\EntityManager" for the service "user.security.login_form_authenticator". Multiple services exist for this class (doctrine.orm.prism_entity_manager, doctrine.orm.baan_entity_manager).
Run Code Online (Sandbox Code Playgroud)

这是我在config.yml中的Doctrine配置

doctrine:
    dbal:
        connections:
            prism:
                driver:   pdo_mysql
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
                # if using pdo_sqlite as your database driver:
                #   1. add the path in parameters.yml
                #     e.g. database_path: "%kernel.root_dir%/../var/data/data.sqlite"
                #   2. Uncomment database_path in parameters.yml.dist
                #   3. Uncomment next line:
                #path:     "%database_path%"
            baan:
                driver:   oci8
                host:     "%baan_host%"
                port:     "%baan_port%"
                dbname:   "%baan_db_name%"
                user:     "%baan_user%"
                password: "%baan_password%"
                charset:  AL32UTF8

    orm:
        default_entity_manager: prism
        auto_generate_proxy_classes: "%kernel.debug%"
        entity_managers:
            auto_mapping: true
            prism:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                connection: prism
                mappings:
                    UserBundle:
                        type: annotation

            baan:
                connection: baan
                mappings:
                    BaanBundle:
                        type: annotation
Run Code Online (Sandbox Code Playgroud)

这是我的Authenticator类中的构造函数

 private $formFactory;

    private $em;

    private $router;


    public function __construct(FormFactoryInterface $formFactory, EntityManager $em, RouterInterface $router)
    {
        $this->formFactory = $formFactory;
        $this->em = $em;
        $this->router = $router;
    }
Run Code Online (Sandbox Code Playgroud)

小智 9

您可以扩展Doctrine EntityManagerDecorator,它实现EntityManagerInterface并接受EntityManager其构造函数中的实例.

首先EntityManagerDecorator为每个连接扩展一次类.

namespace MyBundle\Service\Database;

use Doctrine\ORM\Decorator\EntityManagerDecorator;

class PrismEntityManager extends EntityManagerDecorator {}

class BaanEntityManager extends EntityManagerDecorator {}
Run Code Online (Sandbox Code Playgroud)

然后在您的服务配置中,您需要手动连接这两个服务.

MyBundle\Service\Database\PrismEntityManager:
    arguments:
        $wrapped: '@doctrine.orm.prism_entity_manager'

MyBundle\Service\Database\BaanEntityManager:
    arguments:
        $wrapped: '@doctrine.orm.baan_entity_manager'
Run Code Online (Sandbox Code Playgroud)

现在您只需要为这些服务之一键入提示.

public function __construct(FormFactoryInterface $formFactory, PrismEntityManager $em, RouterInterface $router)
{
    $this->formFactory = $formFactory;
    $this->em = $em;
    $this->router = $router;
}
Run Code Online (Sandbox Code Playgroud)


Mau*_*iya 2

我不知道我是否正确理解你的问题,但你可以为不同的数据库连接设置不同的配置,如下所示:

dbal:
    default_connection: default
    connections:
        default:
            driver:   pdo_mysql
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
            mapping_types:
            enum: smallint
        custom:
            driver:   pdo_mysql
            host:     '%database_host2%'
            port:     '%database_port2%'
            dbname:   '%database_name2%'
            user:     '%database_user2%'
            password: '%database_password2%'
            charset:  UTF8
            mapping_types:
            enum: smallint
    orm:
        default_entity_manager: default
        auto_generate_proxy_classes: "%kernel.debug%"
        entity_managers:
            auto_mapping: true
            default:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                connection: default
                mappings:
                    EntityBundle:
                        type: annotation
                        alias: DBAlias
            custom:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                connection: custom
                mappings:
                    EntityBundle:
                        type: annotation
                        alias: DBAlias
Run Code Online (Sandbox Code Playgroud)

现在您可以使用 传递您的自定义 EntityManager doctrine.orm.custom_entity_manager