Symfony 3-具有多个db连接的EntityManager依赖项注入
我已经使用guard设置了一个自定义验证器,并自动连接了该服务。这是经过测试的,只需配置MySQL即可正常工作 我现在已经指定了第二个数据库连接(oracle),但Symfony现在不允许在我的服务配置中进行自动连接,因为它不知道在将EntityManager注入到自定义验证器类时要使用哪个数据库连接 知道如何配置依赖项注入以使用特定的数据库连接,以便继续使用AutoWire吗Symfony 3-具有多个db连接的EntityManager依赖项注入,symfony,dependency-injection,entitymanager,symfony-3.2,Symfony,Dependency Injection,Entitymanager,Symfony 3.2,我已经使用guard设置了一个自定义验证器,并自动连接了该服务。这是经过测试的,只需配置MySQL即可正常工作 我现在已经指定了第二个数据库连接(oracle),但Symfony现在不允许在我的服务配置中进行自动连接,因为它不知道在将EntityManager注入到自定义验证器类时要使用哪个数据库连接 知道如何配置依赖项注入以使用特定的数据库连接,以便继续使用AutoWire吗 Unable to autowire argument of type "Doctrine\ORM\EntityMan
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中的条令配置
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类中的构造函数
private $formFactory;
private $em;
private $router;
public function __construct(FormFactoryInterface $formFactory, EntityManager $em, RouterInterface $router)
{
$this->formFactory = $formFactory;
$this->em = $em;
$this->router = $router;
}
我不知道我是否正确理解了您的问题,但您可以为不同的数据库连接设置不同的配置,如下所示:
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
现在,您可以使用
doctrine.orm.custom\u entity\u manager
传递自定义EntityManager,您可以扩展doctrine的EntityManager编辑器
,它实现EntityManager接口
,并在其构造函数中接受EntityManager
的实例
首先,为每个连接扩展一次EntityManagerCorator
类
名称空间MyBundle\Service\Database;
使用条令\ORM\Decorator\entitymanager\corator;
类PrimentItyManager扩展EntityManagerDecorator{}
类BaanEntityManager扩展了EntityManagerDecorator{}
然后在服务配置中,您需要手动连接这两个服务
MyBundle\Service\Database\PrismEntityManager:
arguments:
$wrapped: '@doctrine.orm.prism_entity_manager'
MyBundle\Service\Database\BaanEntityManager:
arguments:
$wrapped: '@doctrine.orm.baan_entity_manager'
现在您只需为其中一个服务键入提示
MyBundle\Service\Database\PrismEntityManager:
arguments:
$wrapped: '@doctrine.orm.prism_entity_manager'
MyBundle\Service\Database\BaanEntityManager:
arguments:
$wrapped: '@doctrine.orm.baan_entity_manager'
public function\uu构造(FormFactoryInterface$formFactory,PrismEntityManager$em,RouterInterface$router)
{
$this->formFactory=$formFactory;
$this->em=$em;
$this->router=$router;
}
我想,我在DBAL连接方面遇到了与EntityManager相同的问题。我已经用一些代理类解决了这个问题,如下面的回答所述:
任何问题请告诉我。我已将我的条令配置和类构造函数添加到问题中。如何在我的类构造函数中将“prism”配置传递给$em?您可以将您的类注册到服务中,然后将
@doctrine.orm.prism\u entity\u manager
作为参数传递。请告诉我它是否对您有效?提前感谢:)我的构造函数中有三个参数。我可以混合使用autowire和传递的参数吗,还是只需要执行其中一个?我认为您需要从注册类的位置定义参数,因为如果存在多个相同的类,那么您需要使用参数来定义它们,而不是autowire
,请查看更多详细信息。而且检查也很容易。不要使用自动连线。我认为实际上可能有一个解决办法(检查文档),但为什么还要添加更多的魔法呢。