Symfony 3-具有多个数据库连接的EntityManager依赖项注入 [英] Symfony 3 - EntityManager dependency injection with multiple db connections
问题描述
我已经设置了使用Guard的Custom Authenticator,并自动为服务连线。
I have setup a Custom Authenticator using guard and auto wired the service. This is tested and works fine with just MySQL configured.
我现在指定了第二个数据库连接(oracle),但是Symfony现在不允许在我的服务配置中自动装配,因为它不知道将EntityManager注入自定义Authenticator类时要使用哪个数据库连接。
I have now specified a second database connection (oracle), but Symfony will now not allow autowiring in my service configuration, because it does not know which database connection to use when injecting EntityManager in to the custom Authenticator class.
任何想法我都可以配置依赖注入以使用特定的数据库连接因此我可以继续使用AutoWire。
Any idea how I can Configure the Dependency Injection to use a specific database connection so I can continue to use 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).
这是我在config.yml中的教义配置
Here is my Doctrine config in config.yml
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
这里是我的Authenticator类中的构造函数
Here is the constructor in my Authenticator class
private $formFactory;
private $em;
private $router;
public function __construct(FormFactoryInterface $formFactory, EntityManager $em, RouterInterface $router)
{
$this->formFactory = $formFactory;
$this->em = $em;
$this->router = $router;
}
推荐答案
您可以扩展Doctrine的 EntityManagerDecorator
,它实现 EntityManagerInterface
并在其内部接受 EntityManager
的实例
You can extend Doctrine's EntityManagerDecorator
, which implements EntityManagerInterface
and accepts an instance of EntityManager
in its constructor.
首先为每个连接扩展 EntityManagerDecorator
类一次。
First extend the EntityManagerDecorator
class once for each of your connections.
namespace MyBundle\Service\Database;
use Doctrine\ORM\Decorator\EntityManagerDecorator;
class PrismEntityManager extends EntityManagerDecorator {}
class BaanEntityManager extends EntityManagerDecorator {}
然后在服务配置中,您需要手动连接这两个服务。
Then in your service configuration, you need to manually wire these two services.
MyBundle\Service\Database\PrismEntityManager:
arguments:
$wrapped: '@doctrine.orm.prism_entity_manager'
MyBundle\Service\Database\BaanEntityManager:
arguments:
$wrapped: '@doctrine.orm.baan_entity_manager'
现在,您只需
public function __construct(FormFactoryInterface $formFactory, PrismEntityManager $em, RouterInterface $router)
{
$this->formFactory = $formFactory;
$this->em = $em;
$this->router = $router;
}
这篇关于Symfony 3-具有多个数据库连接的EntityManager依赖项注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!