简体   繁体   中英

Doctrine custom hydrators with Zend Framework 2 - How to specify in config file?

I've successfully created a custom Doctrine Hydrator, and can use it by calling the

$entityManager->getConfiguration()->addCustomHydrationMode('...', '...')

method.

However, I'm injecting dependencies into constructors via factories, so the entity manager isn't available in the classes themselves - which makes inheritance a bit more annoying (factory inheritance anyone?).

What I would prefer is to be able to configure my custom hydrators in the zf2 config file (where all the routes, services, other doctrine stuff etc is configured) - but I can't find any documentation on how to specify this.

  1. Is this even possible?
  2. What are the magic incantations necessary?

Cheers

It's possible to register hydrators in doctrine via configuration. For example to register the DoctrineModule\\Stdlib\\Hydrator\\DoctrineObject hydrator, in your module configuration array which is loaded by Application\\Module::getConfig you can add the following code.

'doctrine' => array(
    'configuration' => array(
        'orm_default' => array(
            'customHydrationModes' => array(
                'DoctrineModule' => 'DoctrineModule\Stdlib\Hydrator\DoctrineObject',
            ),
        ),
    ),
),

This registers DoctrineModule\\Stdlib\\Hydrator\\DoctrineObject under an hydration mode called DoctrineModule . Now you can obtain this hydrator from the entity manager.

$entityManager->newHydrator('DoctrineModule');

For more information about the DoctrineModule see https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md .

Found the way to specify custom doctrine hydrators in the config file:

['doctrine']['configuration']['orm_default']['customHydrationModes']

From the class docs (\\DoctrineORMModule\\Options\\Configuration):

Keys must be the name of the custom hydration method and the value must be the class name for the custom hydrator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM