'Symfony: access property of EntityManagerDecorator

I need to decorate an Symfony EntityManager and access a property that I inject in it from ouside, but it always says that this is Undefined Property.

This is config:

    app.decorated.doctrine.orm.default_entity_manager:
    class: App\Decorator\EntityManager
    public: false
    decorates: "doctrine.orm.default_entity_manager"
    arguments:
        - "@app.decorated.doctrine.orm.default_entity_manager.inner"
        - '%photo_directory_absolute%'

So for example Im passing value '%photo_directory_absolute%'

This is part of decorator:

    namespace App\Decorator;
    class EntityManager extends EntityManagerDecorator
    {
        public $photoDir;
        public function __construct(EntityManagerInterface $wrapped, string $photoDir)
        {
            parent::__construct($wrapped);
            $this->photoDir = $photoDir;    
        }

Then I want to get this 'photoDir' value from Hydrator

namespace App\Hydrator;

class UserAppHydrator  extends ObjectHydrator
{   
    protected function hydrateRowData(array $data, array &$result)
    {
        $hydrated_result = array();
        parent::hydrateRowData($data, $hydrated_result);

       die(var_dump($this->_em->photoDir));

And this shows me an error :

"Notice: Undefined property: Doctrine\\ORM\\EntityManager::$photoDir"

What I am missing? Thanks



Solution 1:[1]

You have to override the method newHydrator of the EntityManagerDecorator to get your EntityManager injected into a Hydrator. If that is missing, the $wrapped instance will be used.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Powerhamster