0

I did following the following tutorial for custom hydration. https://techpunch.co.uk/development/create-custom-doctrine2-hydrator-symfony2

What I'm not happy with it. I dont want that the user of my bundle need to configure the hydrator in there config.yml.

Is it possible to add it as a service and tag it e.g.

<service id="bundle.hydration.custom" class="Your\Bundle\Hydrators\ListHydrator">
    <argument type="service" id="helper_service" />

    <tag name="doctrine.hydrator" alias="ListHydrator" />
</service>

Also I need other services in my hydrator so I need to register it as service.

Alexander Schranz
  • 2,154
  • 2
  • 25
  • 42

1 Answers1

1

If custom hydrator has no dependencies besides entity manager, then you can get the definition of "doctrine.orm% name% _configuration" during container compilation and inject class name:

$container
    ->getDefinition('doctrine.orm.some_configuration') // replace "some" by your entity manager name
    ->addMethodCall('addCustomHydrationMode', [
        'custom_custom', 'SomeCustomHydratorClass'
    ]);

But if hydrator depends on other services it is a problem: Doctrine instantiate a hydrators itself:

// Doctrine\ORM\EntityManager::newHydrator($hydrationMode)

if (($class = $this->config->getCustomHydrationMode($hydrationMode)) !== null) {
    return new $class($this);
}

So, perhaps you need to create a custom EntityManager and override newHydrator() method.

Hope this helps.

Community
  • 1
  • 1
Timurib
  • 2,735
  • 16
  • 29