@@ -81,28 +81,42 @@ public function dehydrate(object $object): mixed
8181
8282 private function objectManagerFor (string $ class ): ?ObjectManager
8383 {
84- if (!class_exists ($ class ) && !interface_exists ($ class )) {
84+ if (class_exists ($ class )) {
85+ // Keep the standard way for a class
86+ // todo cache/warmup an array of classes that are "doctrine objects"
87+ foreach ($ this ->managerRegistries as $ registry ) {
88+ if ($ om = $ registry ->getManagerForClass ($ class )) {
89+ return self ::ensureManagedObject ($ om , $ class );
90+ }
91+ }
8592 return null ;
8693 }
8794
88- // todo cache/warmup an array of classes that are "doctrine objects"
89- foreach ($ this ->managerRegistries as $ registry ) {
90- // The doctrine registry does not resolve aliased interface
91- // if ($om = $registry->getManagerForClass($class)) {
92- // return self::ensureManagedObject($om, $class);
93- // }
94- foreach ($ registry ->getManagers () as $ om ) {
95- // But we can resolve nicely by trying to ask each manager to get the metadata
96- try {
97- if (null !== $ om ->getClassMetadata ($ class )) {
98- return self ::ensureManagedObject ($ om , $ class );
95+ if (interface_exists ($ class )) {
96+ // special handler for interfaces
97+ // For use cases : @see https://symfony.com/doc/current/doctrine/resolve_target_entity.html
98+ // As today, getManagerForClass don't resolve nicely aliased interfaces
99+ // The workaround is to enum over each object manager and trying to get metadata
100+ // The metadata are indeed, resolved nicely
101+ // Fore more details :
102+ // @see \Doctrine\ORM\Tools\ResolveTargetEntityListener
103+
104+ // todo cache/warmup an array of interfaces that are resolved "doctrine objects"
105+ foreach ($ this ->managerRegistries as $ registry ) {
106+ foreach ($ registry ->getManagers () as $ om ) {
107+ // the getClassMetaData can indeed throw an exception
108+ // so, we
109+ try {
110+ if (null !== $ om ->getClassMetadata ($ class )) {
111+ return self ::ensureManagedObject ($ om , $ class );
112+ }
113+ } catch (MappingException $ e ) {
114+ // I did not find a nice way to check if it is because the class is really unknown
115+ // It is good to check for a specific exception ?
116+ // eg: \Doctrine\Persistence\Mapping\MappingException
117+ // @see \Doctrine\Persistence\Mapping\AbstractClassMetadataFactory::getMetadataFor
118+ // throw $e;
99119 }
100- } catch (MappingException $ e ) {
101- // I did not find a nice way to check if it is because the class is really unknown
102- // It is good to check for a specific exception ?
103- // eg: \Doctrine\Persistence\Mapping\MappingException
104- // Maybe not needed, because it does not failed even when the class does not exist at all
105- // throw $e;
106120 }
107121 }
108122 }
0 commit comments