Open
Description
- Using symfony 7.3
With Doctrine ORM, when aliasing a concrete class to an interface, the hydratation process does not work anymore.
Doctrine configuration
doctrine:
orm:
resolve_target_entities:
MyInterface: App\Entity\MyRealEntity
In the component
#[LiveProp()]
public ?MyInterface $initialFormData = null;
The error
An exception has been thrown during the rendering of a template ("Cannot dehydrate value typed as interface
The workaround
I have a workaround but i'mnot really used about what to do to help, so, guidelines are welcome :)
Digging into DoctrineEntityHydrationExtension.php :
private function objectManagerFor(string $class): ?ObjectManager
{
if (!interface_exists($class) && !class_exists($class)) {
return null;
}
// todo cache/warmup an array of classes that are "doctrine objects"
foreach ($this->managerRegistries as $registry) {
foreach($registry->getManagers() as $om) {
// this way, it resolve the interface
if ($om->getClassMetadata($class)) {
return self::ensureManagedObject($om, $class);
}
}
// does not work with interface
// if ($om = $registry->getManagerForClass($class)) {
// return self::ensureManagedObject($om, $class);
// }
}
return null;
}