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

Closed
opened 2026-01-22 15:44:13 +01:00 by admin · 0 comments
Owner

Originally created by @noemi-salaun on GitHub (Dec 21, 2022).

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

Originally created by @noemi-salaun on GitHub (Dec 21, 2022). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | 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. ```php enum MyEnum: string { case Yellow = 'yellow'; case Blue = 'blue'; } ``` ```php #[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; } ``` ```php #[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; } ``` ```mysql 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 ```php 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(); } ``` <!-- Provide steps to reproduce the bug. If possible, also add a code snippet with relevant configuration, entity mappings, DQL etc. Adding a failing Unit or Functional Test would help us a lot - you can submit one in a Pull Request separately, referencing this bug report. --> #### Expected behavior <!-- What was the expected (correct) behavior? --> No error
admin added the Bug label 2026-01-22 15:44:13 +01:00
admin closed this issue 2026-01-22 15:44:14 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7080