2012-03-12 25 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