persistence icon indicating copy to clipboard operation
persistence copied to clipboard

Allow ORM to use `ResolveTargetEntityListener` in ManagerRegistry::getManagerForClass()

Open mpdude opened this issue 4 years ago • 1 comments

I am not quite sure if this is the right place to ask, because the change aims at a feature of ORM. Anyways, here it goes:

Doctrine ORM features a ResolveTargetEntityListener. This mechanism allows to use Interface names in various places, for example in Association Mappings or when querying Repositories from Entity Managers.

It would be helpful if the AbstractManagerRegistry could support looking up Managers by such interfaces as well.

The problem with the current implementation is that it makes use of the isTransient property. Probably this happens to speed up things: Inside the Metadata Factory, the Driver can determine isTransient() more easily, without loading the full metadata.

Interfaces, however, are (to my understanding) always transient, so getManagerForClass() never works for them. This is where this PR hopes to improve things without affecting performance for existing use cases.

Will add tests once I get basic approval and :+1:s.

Background: ManagerRegistry::getManagerForClass() is used inside https://github.com/sensiolabs/SensioFrameworkExtraBundle for the Symfony Framework. So-called "ParamConverters" can automagically fetch (ORM) Entities for request parameters and pass those into Controller classes, reducing boilerplate code. It would be great if we could make this work as well for "resolved target entities".

mpdude avatar Dec 03 '20 16:12 mpdude

I don't think this will work with more than one entity managers. Maybe something like this will work better:

        $metadataFactory = $manager->getMetadataFactory();

        if (! $metadataFactory->isTransient($class) || $metadataFactory->hasMetadataFor($class)) {
            return $manager;
        }

esmultivac avatar Oct 13 '22 05:10 esmultivac