Skip to content

ObjectHydrator::getEntityFromIdentityMap() does not work with backed enum #10334

@noemi-salaun

Description

@noemi-salaun

Bug Report

Q A
BC Break no
Version 2.14.0

Summary

I have an entity with a backed enum as primary key. When I fetch a fresh entity from the database, everything works fine. But if I try to fetch an entity that is already known in the identity map, it fails.

Current behavior

It cannot load an entity with backed enum as primary key from the ObjectHydrator identity map.

Error "Object of class MyEnum could not be converted to string"

From ObjectHydrator on line 292

How to reproduce

We need 2 entities, with ManyToOne relation, so we can use a join in our request.

enum MyEnum: string
{
    case Yellow = 'yellow';
    case Blue = 'blue';
}
#[ORM\Entity, ORM\Table(name: 'foo')]
class Foo
{
    #[ORM\Id, ORM\ManyToOne(inversedBy: 'foos'), ORM\JoinColumn(nullable: false)]
    private readonly FooCollection $collection;

    #[ORM\Id, ORM\Column]
    private readonly MyEnum $myEnum;
}
#[ORM\Entity, ORM\Table(name: 'foo_collection')]
class FooCollection
{
    #[ORM\Id, ORM\Column]
    private int $id;

    /** @var Collection<int, Foo> */
    #[ORM\OneToMany(targetEntity: Foo::class, mappedBy: 'collection')]
    private Collection $foos;
}
create table foo_collection
(
    id int not null primary key
);

create table foo
(
    collectionId int          not null,
    myEnum       varchar(255) not null,
    primary key (collectionId, myEnum),
    constraint foo_collection_id_fk
        foreign key (collectionId) references foo_collection (id)
);

INSERT INTO foo_collection (id) VALUES (1);
INSERT INTO foo (collectionId, myEnum) VALUES (1, 'yellow'), (1, 'blue');

And then, just run 2 queries to trigger the loading from the identity map

    public function test(EntityManagerInterface $entityManager): void
    {
        $entityManager
            ->getRepository(FooCollection::class)
            ->createQueryBuilder('collection')
            ->leftJoin('collection.foos', 'foo')->addSelect('foo')
            ->getQuery()
            ->getResult();

        $entityManager
            ->getRepository(FooCollection::class)
            ->createQueryBuilder('collection')
            ->leftJoin('collection.foos', 'foo')->addSelect('foo')
            ->getQuery()
            ->getResult();
    }

Expected behavior

No error

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