2012-03-12 91 views
3

我正在将Symfony DIC集成到zend框架应用程序中,除了父服务之外,这很好。Symfony DIC和父服务不起作用

在我的DIC配置中,我有一个父服务PC_Service,它将被我的所有服务扩展。 问题是实体管理器在扩展PC_Service的服务中不可用(NULL)。当我通过service.stats注入entitymanager时,entitymanger设置正确。

... 
<service id="pc.service" class="PC_Service" abstract="true"> 
    <call method="setEntityManager"> 
     <argument type="service" id="doctrine.entitymanager" /> 
    </call> 
</service> 
... 
<service id="service.stats" class="Application_Service_Stats" parent="pc.service" /> 
... 

PC_Service

abstract class PC_Service 
{ 
    protected $_em; 

    public function setEntityManager($entityManager) 
    { 
     $this->_em = $entityManager; 
    } 
} 

Application_Service_Stats

class Application_Service_Stats extends PC_Service 
{ 
    ... $this->_em should be set here. 
} 

我希望有人能告诉我什么,我做错了。

+0

有没有比使用symfony2更简单的将学说融入zend的方法? – Ascherer 2012-03-12 06:11:57

+1

这不是关于整合原则,而是关于依赖注入 – tom 2012-03-12 06:14:06

+0

你最终找到解决方案吗?我遇到了同样的问题,并且正在通过Symfony代码进行分析(不幸的是,至今没有多少运气) – 2012-05-19 17:34:55

回答

-1

解决的办法是编译服务容器附近的ZF引导结束。这个过程有一个叫做ResolveDefinitionTemplatesPass的步骤,它在来自父服务的调用中打补丁。

这通常由Symfony Kernel完成,但它当然不会出现在ZF集成中。

protected function _initServiceContainerCompilation() 
{ 
    // Wait for the SC to get built 
    $this->bootstrap('Services'); 

    // Doctrine modifies the SC, so we need to wait for it also 
    $this->bootstrap('Doctrine'); 

    // Compiling the SC allows "ResolveDefinitionTemplatesPass" to run, 
    // allowing services to inherit method calls from parents 
    $sc = $this->getResource('Services'); 
    $sc->compile(); 
} 
+0

服务容器已正确初始化,它工作正常。 – tom 2012-05-21 09:11:53

+0

是的,除了父母的电话没有被继承,我的方法也是正确的。你确定你在做'ServiceContainer-> compile()'吗?由于您在Symfony内核之外使用SC,因此您需要执行额外的步骤。 – 2012-05-21 12:59:00

+0

如果你正在调试,你甚至可以在'Symfony \ Component \ DependencyInjection \ Compiler \ ResolveDefinitionTemplatesPass-> process()'中设置一个断点来查看它是否被调用。这是负责管理服务继承的方法。 – 2012-05-21 13:00:09

1

不知道如果它是一个错字,但它应该是doctrine.orm.default_entity_managerdoctrine.orm.entity_manager(在previuos的别名):

<service id="pc.service" class="PC_Service" abstract="true"> 
    <call method="setEntityManager"> 
     <argument type="service" id="doctrine.orm.default_entity_manager" /> 
    </call> 
</service> 
+0

不,我的entitymanger被命名为doctrine.entitymanager – tom 2012-03-12 15:14:20

+0

@tom请发布doctrine.entitymanager的定义。 – Polmonino 2012-03-12 16:10:19

+0

doctrine.entitymanager不是问题所在。如果我直接将它注入service.stats – tom 2012-03-12 17:23:37