Delegator Factories in Zend Framework 2 are a powerful tool to hook into the creating of standard ZF objects and change or even completely replcae them by custom ones.
I want to create a Delegator Factory for a Hydrator, in order to attach some Hydrator Strategies to it. But it's not working yet...
module.config.php
return array(
...
'service_manager' => array(
...
'invokables' => array(
...
'AuthenticationAdapterDelegatorFactory' => 'Foo\\MvcAuth\\Factory\\AuthenticationAdapterDelegatorFactory', // <-- it works
'DoctrineObjectHydratorDelegatorFactory' => 'Bar\\Model\\Entity\\Hydrator\\DoctrineObjectHydratorDelegatorFactory', // <-- it doesn't
),
'delegators' => array(
'ZF\\MvcAuth\\Authentication\\DefaultAuthenticationListener' => array( // <-- it works
0 => 'AuthenticationAdapterDelegatorFactory',
),
'DoctrineModule\\Stdlib\\Hydrator\\DoctrineObject' => array( // <-- it doesn't
0 => 'DoctrineObjectHydratorDelegatorFactory'
)
),
),
...
);
Since hydrators are actually provided by the HydratorPluginManager
, I've also tried it this way:
return array(
'service_manager' => array(
...
'invokables' => array(
...
'DoctrineObjectHydratorDelegatorFactory' => 'Bar\\Model\\Entity\\Hydrator\\DoctrineObjectHydratorDelegatorFactory',
),
'delegators' => array(
...
'DoctrineModule\\Stdlib\\Hydrator\\DoctrineObject' => array( // <-- it doesn't
0 => 'DoctrineObjectHydratorDelegatorFactory'
)
),
),
'hydrator_manager' => array(
'delegators' => array(
'DoctrineModule\\Stdlib\\Hydrator\\DoctrineObject' => array(
0 => 'DoctrineObjectHydratorDelegatorFactory'
)
),
),
);
So what is going wrong here and how can I register a Delegator Factory for a Hydrator in Zend Framework 2?
You are totally right you have to register the delegators for your hydrator classes inside the HydratorPluginManager
. The problem lies in the key name in your module.config.php
.
The key to use is hydrators
and not like you have hydrator_manager
:
'hydrators' => array(
'delegators' => array(
//...your delegators config...
)
),
I personally think the naming of keys in the ZF2 config arrays is very inconsistent and confusing. Keep this in mind if you have similar problems like this again.
Check also this page from Rob Allen as a reference for all other service manager configuration keys.