Skip to content

Problem while hydrating a Doctrine Entity when using an interface as alias. #2832

Open
@pink6440

Description

@pink6440
  • 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;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions