Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
noemi-salaun opened this issue Dec 21, 2022 · 0 comments · Fixed by #10508
Closed

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

noemi-salaun opened this issue Dec 21, 2022 · 0 comments · Fixed by #10508
Labels

Comments

@noemi-salaun
Copy link
Contributor

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

@derrabus derrabus added the Bug label Jan 4, 2023
Gwemox added a commit to Gwemox/orm that referenced this issue Feb 10, 2023
When an entity have a backed enum as identifier, `UnitOfWork` tries to
cast to string when generating the hash of the id.
This fix call `->value` when identifier is a `BackedEnum`.
Fixes doctrine#10471
Fixes doctrine#10334
Gwemox added a commit to Gwemox/orm that referenced this issue Feb 10, 2023
When an entity have a backed enum as identifier, `UnitOfWork` tries to
cast to string when generating the hash of the id.
This fix calls `->value` when identifier is a `BackedEnum`.
Fixes doctrine#10471
Fixes doctrine#10334
Gwemox added a commit to Gwemox/orm that referenced this issue Feb 10, 2023
When an entity have a backed enum as identifier, `UnitOfWork` tries to
cast to string when generating the hash of the id.
This fix calls `->value` when identifier is a `BackedEnum`.
Fixes doctrine#10471
Fixes doctrine#10334
Gwemox added a commit to Gwemox/orm that referenced this issue Feb 10, 2023
When an entity have a backed enum as identifier, `UnitOfWork` tries to
cast to string when generating the hash of the id.
This fix calls `->value` when identifier is a `BackedEnum`.
Fixes doctrine#10471
Fixes doctrine#10334
Gwemox added a commit to Gwemox/orm that referenced this issue Feb 10, 2023
When an entity have a backed enum as identifier, `UnitOfWork` tries to
cast to string when generating the hash of the id.
This fix calls `->value` when identifier is a `BackedEnum`.
Fixes doctrine#10471
Fixes doctrine#10334
Gwemox added a commit to Gwemox/orm that referenced this issue Feb 10, 2023
When an entity have a backed enum as identifier, `UnitOfWork` tries to
cast to string when generating the hash of the id.
This fix calls `->value` when identifier is a `BackedEnum`.
Fixes doctrine#10471
Fixes doctrine#10334
Gwemox added a commit to Gwemox/orm that referenced this issue Feb 11, 2023
When an entity have a backed enum as identifier, `UnitOfWork` tries to
cast to string when generating the hash of the id.
This fix calls `->value` when identifier is a `BackedEnum`.
Fixes doctrine#10471
Fixes doctrine#10334
Gwemox added a commit to Gwemox/orm that referenced this issue Feb 14, 2023
When an entity have a backed enum as identifier, `UnitOfWork` tries to
cast to string when generating the hash of the id.
This fix calls `->value` when identifier is a `BackedEnum`.
Fixes doctrine#10471
Fixes doctrine#10334
Gwemox added a commit to Gwemox/orm that referenced this issue Feb 20, 2023
When an entity have a backed enum as identifier, `UnitOfWork` tries to
cast to string when generating the hash of the id.
This fix calls `->value` when identifier is a `BackedEnum`.
Fixes doctrine#10471
Fixes doctrine#10334
Gwemox added a commit to Gwemox/orm that referenced this issue Apr 3, 2023
When an entity have a backed enum as identifier, `UnitOfWork` tries to
cast to string when generating the hash of the id.
This fix calls `->value` when identifier is a `BackedEnum`.
Fixes doctrine#10471
Fixes doctrine#10334
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants