Symfony DIC and parent services not working
I am integrating Symfony DIC into zend platform app which works great except for parent services.
In my DIC config, I have a parent service PC_Service that will be extended by all my services. The problem is that the entity manager is not available (NULL) in services that extend PC_Service. When I inject the entitymanager via service.stats the entitymanger is installed correctly.
...
<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.
}
I hope someone tells me what I am doing wrong.
source to share
The solution is to compile the service container towards the end of the ZF boot buffer. This process has a step called ResolveDefinitionTemplatesPass
that fixes calls from parent services.
This is usually done with the Symfony Kernel , but of course it is missing from the ZF integration.
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();
}
source to share
Don't know if this is a typo, but it should be doctrine.orm.default_entity_manager
or doctrine.orm.entity_manager
(previuos alias):
<service id="pc.service" class="PC_Service" abstract="true">
<call method="setEntityManager">
<argument type="service" id="doctrine.orm.default_entity_manager" />
</call>
</service>
source to share