我配置了2个entityManager并且工作正常(所有映射都正确加载等),这使我能够使用2个数据库。
与标准Doctrine Command任务允许-em或--connection参数的方式类似,我想有效地指定EntityManager,从而指定使用的DB。
我已经完成了在命令中切换容器引用的显而易见的事情:
if ($input->getOption('background')) {
$this->container->set('doctrine.orm.entity_manager', $this->container->get('doctrine.orm.background_entity_manager'));
}
这适用于前x个db查询(是一个长时间运行的命令)但是当Doctrine执行重新连接时,默认的EntityManager似乎被使用而另一个db连接到:
2016-04-07T11:27:47.792427Z 783 Connect root@10.20.1.1 on partridge_background using TCP/IP
2016-04-07T11:27:47.802813Z 783 Query SELECT t0.id AS id_1, t0.name AS name_2, t0.last_completion AS last_completion_3, t0.last_run AS last_run_4, t0.bookie_id AS bookie_id_5 FROM feeds t0 WHERE t0.name = 'CORAL_OPENBET' LIMIT 1
2016-04-07T11:27:47.823585Z 783 Query SELECT c0_.id AS id_0, c0_.name AS name_1, c0_.canonicalised_name AS canonicalised_name_2, c0_.matchable_name AS matchable_name_3, c0_.display_order AS display_order_4, c0_.filtered AS filtered_5, c0_.name_alias AS name_alias_6, c0_.created_at AS created_at_7, c0_.updated_at AS updated_at_8, m1_.id AS id_9, m1_.domain_id AS domain_id_10, m1_.feed_id AS feed_id_11, m1_.feed_element_id AS feed_element_id_12, m1_.feed_request_url AS feed_request_url_13, m1_.feed_last_updated AS feed_last_updated_14, c0_.category2_id AS category2_id_15, m1_.domain_id AS domain_id_16 FROM category3 c2_, category3 c0_ INNER JOIN metacategory3 m1_ ON c0_.id = m1_.domain_id WHERE c0_.filtered = 0
2016-04-07T11:27:49.057129Z 784 Connect root@10.20.1.1 on partridge using TCP/IP
2016-04-07T11:27:49.060680Z 784 Query SELECT t0.id AS id_1, t0.name AS name_2, t0.canonicalised_name AS canonicalised_name_3, t0.matchable_name AS matchable_name_4, t0.display_order AS display_order_5, t0.filtered AS filtered_6, t0.name_alias AS name_alias_7, t0.created_at AS created_at_8, t0.updated_at AS updated_at_9, t0.category2_id AS category2_id_10 FROM category3 t0 WHERE t0.id = 48
2016-04-07T11:27:49.065852Z 784 Query SELECT t0.id AS id_1, t0.domain_id AS domain_id_2, t0.feed_id AS feed_id_3, t0.feed_element_id AS feed_element_id_4, t0.feed_request_url AS feed_request_url_5, t0.feed_last_updated AS feed_last_updated_6, t0.domain_id AS domain_id_7 FROM metacategory3 t0 WHERE t0.domain_id = 48
在框架生命周期中相对较晚地执行此EntityManager切换显然是错误的方法。我想要的是某种Symfony引导事件监听器,它可以根据请求更改默认的EntityManager(在这种情况下和ArgvInput参数,但如果可以监听任何类型的请求的参数,那将是很好的。)
我正在考虑在运行时进行某种动态配置操作?
答案 0 :(得分:0)
给予一些思考和一些研究(https://github.com/symfony/symfony/issues/1437)使我得出这样的结论:不可能改变容器在运行时可以合理返回的服务,因为它是"&#34 ;编译"性质。似乎解决方案是使用自己的配置创建一个新环境,并在调用我的命令时使用--env开关。