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;
}
Activity
[-]Problem white Hydrating a Doctrine Entity when using an interface as alias.[/-][+]Problem while hydrating a Doctrine Entity when using an interface as alias.[/+]Kocal commentedon Jun 11, 2025
I'm far from being a Doctrine expert, and at this moment I don't know how we can use this
resolve_target_entities
setting / Doctrine API to get yourMyInterface
being correctly hydrated.As a short solution, I can suggest you to create your own Hydration Extension:
MyInterface
MyInterface
entityMyInterface
entity to an idpink6440 commentedon Jun 12, 2025
Hi Kocal,
I'm agree about create a specific hydrator.
However, it is still possible to make the current doctrine hydrator to work if objectManagerFor method is updated.
First problem to solve : find the object manager wich is responsible for the final entity
Second problem : how to hydrate/dehydrate the entity from the interface
According to the existing code of the doctrine hydrator, it already works.
And same goes for hydrate process (the find method resolves nicely if using interface)
Kocal commentedon Jun 12, 2025
Hi, and thanks for your investigation :)
Do you want to open a PR?
pink6440 commentedon Jun 12, 2025
Thanks you too for the already done work !
And I will try to make the pm.