Enum fields can't be an id field. #7061

Closed
opened 2026-01-22 15:43:56 +01:00 by admin · 10 comments
Owner

Originally created by @johannes85 on GitHub (Oct 14, 2022).

Bug Report

Q A
BC Break no
Version 2.13.3

Summary

Id Field with enumType is not working.

Current behavior

I have a entity called "display" with a primary key over multiple columns:
[id] int feedback_id (relation to feedback)
[id] string with enumType identity_type
string identity_id

There also is a parent entity called "feedback" with the following columns:
[id] int id
collection displays (relation to display)

When I query for a entry from feedback with a join to displays, this call failes when done a second time, with:
Object of class App\Entity\IdentityType could not be converted to string
in vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:286

It is caused by this code:

foreach ($class->identifier as $fieldName) {
                $idHash .= ' ' . (isset($class->associationMappings[$fieldName])
                    ? $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']]
                    : $data[$fieldName]);
            }

not expecting $data[$fieldName]) to be an enum.

Expected behavior

Support for enum fields as index.

Originally created by @johannes85 on GitHub (Oct 14, 2022). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | no | Version | 2.13.3 #### Summary Id Field with enumType is not working. #### Current behavior I have a entity called "display" with a primary key over multiple columns: [id] int feedback_id (relation to feedback) [id] string with enumType identity_type string identity_id There also is a parent entity called "feedback" with the following columns: [id] int id collection<display> displays (relation to display) When I query for a entry from feedback with a join to displays, this call failes when done a second time, with: Object of class App\Entity\IdentityType could not be converted to string in vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:286 It is caused by this code: ``` foreach ($class->identifier as $fieldName) { $idHash .= ' ' . (isset($class->associationMappings[$fieldName]) ? $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']] : $data[$fieldName]); } ``` not expecting $data[$fieldName]) to be an enum. #### Expected behavior Support for enum fields as index.
admin closed this issue 2026-01-22 15:43:56 +01:00
Author
Owner

@beberlei commented on GitHub (Oct 15, 2022):

This is the expected behavior at the monent, should work with toString implemented

@beberlei commented on GitHub (Oct 15, 2022): This is the expected behavior at the monent, should work with toString implemented
Author
Owner

@johannes85 commented on GitHub (Oct 18, 2022):

There is no way to implement toString for an enum.

@johannes85 commented on GitHub (Oct 18, 2022): There is no way to implement toString for an enum.
Author
Owner

@beberlei commented on GitHub (Oct 19, 2022):

I did not know / forgot this, hmmm

@beberlei commented on GitHub (Oct 19, 2022): I did not know / forgot this, hmmm
Author
Owner

@michnovka commented on GitHub (Oct 20, 2022):

Why isnt identifier flattener used in this case?

Check https://github.com/doctrine/orm/pull/9629 this feature should have been already implemented and I am using enum IDs in my project without issue.

Please provide test which shows how its broken.

@michnovka commented on GitHub (Oct 20, 2022): Why isnt identifier flattener used in this case? Check https://github.com/doctrine/orm/pull/9629 this feature should have been already implemented and I am using enum IDs in my project without issue. Please provide test which shows how its broken.
Author
Owner

@flight643 commented on GitHub (Nov 3, 2022):

I am also facing this problem. In my case enum field is a part of the composite primary key. Latest working version is 2.13.1.
I checked #10158 would have fixed the problem if the https://github.com/doctrine/orm/pull/10158#discussion_r999745713 was taken into account

@flight643 commented on GitHub (Nov 3, 2022): I am also facing this problem. In my case enum field is a part of the composite primary key. Latest working version is 2.13.1. I checked #10158 would have fixed the problem if the https://github.com/doctrine/orm/pull/10158#discussion_r999745713 was taken into account
Author
Owner

@michnovka commented on GitHub (Nov 3, 2022):

@flight643 please provide broken test case

@michnovka commented on GitHub (Nov 3, 2022): @flight643 please provide broken test case
Author
Owner

@tomudding commented on GitHub (Dec 6, 2022):

I am experiencing the same problem on 2.13.4. While our actual implementation is a bit more complex I have been able to abstract it to a failing test case.

PHPunit gives the following information:

1) Doctrine\Tests\Functional\GH10132Test::testQueryBackedEnumInCompositeKeyJoin
Error: Object of class Doctrine\Tests\Functional\ComplexType could not be converted to string

lib/Doctrine/ORM/UnitOfWork.php:2846
lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:266
lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:492
lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:148
lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:270
lib/Doctrine/ORM/AbstractQuery.php:1225
lib/Doctrine/ORM/AbstractQuery.php:1166
lib/Doctrine/ORM/AbstractQuery.php:953
tests/Doctrine/Tests/ORM/Functional/Ticket/GH10132Test.php:75

L75 of the test case.

I was able to resolve this with a patch that closely resembles the one proposed in #10158 for ObjectHydrator.

@tomudding commented on GitHub (Dec 6, 2022): I am experiencing the same problem on 2.13.4. While our actual implementation is a bit more complex I have been able to abstract it to [a failing test case](https://github.com/tomudding/orm/blob/45d5239b1434cec3ca4051f40316ba6f8eef0701/tests/Doctrine/Tests/ORM/Functional/Ticket/GH10132Test.php). PHPunit gives the following information: ``` 1) Doctrine\Tests\Functional\GH10132Test::testQueryBackedEnumInCompositeKeyJoin Error: Object of class Doctrine\Tests\Functional\ComplexType could not be converted to string lib/Doctrine/ORM/UnitOfWork.php:2846 lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:266 lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:492 lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:148 lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:270 lib/Doctrine/ORM/AbstractQuery.php:1225 lib/Doctrine/ORM/AbstractQuery.php:1166 lib/Doctrine/ORM/AbstractQuery.php:953 tests/Doctrine/Tests/ORM/Functional/Ticket/GH10132Test.php:75 ``` [L75](https://github.com/tomudding/orm/blob/45d5239b1434cec3ca4051f40316ba6f8eef0701/tests/Doctrine/Tests/ORM/Functional/Ticket/GH10132Test.php#L75) of the test case. I was able to resolve this with a patch that closely resembles the one proposed in #10158 for `ObjectHydrator`.
Author
Owner

@michnovka commented on GitHub (Dec 6, 2022):

I was looking into the code, trying to come up with a fix, but I noticed something weird.

SubComplexChild in your example has just one associationMapping, specifically complex_type. All the other mappings are ignored in UoW, i.e. there is no complex_number nor complexChild_point.

It makes me wonder, is this even a valid syntax:

#[ManyToOne(
        targetEntity: ComplexChild::class,
        inversedBy: 'subComplexChildren',
    )]
    #[JoinColumn(
        name: 'complex_type',
        referencedColumnName: 'complex_type',
    )]
    #[JoinColumn(
        name: 'complex_number',
        referencedColumnName: 'complex_number',
    )]
    #[JoinColumn(
        name: 'complexChild_point',
        referencedColumnName: 'point',
    )]
    protected ComplexChild $complexChild;

?

EDIT:

Ok, this syntax was actually OK, but the code is using your PHPDoc syntax and with phpdoc, if you have multiple join columns, you need to wrap them with ORM\JoinColumns. I fixed this in my test

@michnovka commented on GitHub (Dec 6, 2022): I was looking into the code, trying to come up with a fix, but I noticed something weird. SubComplexChild in your example has just one associationMapping, specifically complex_type. All the other mappings are ignored in UoW, i.e. there is no complex_number nor complexChild_point. It makes me wonder, is this even a valid syntax: ```php #[ManyToOne( targetEntity: ComplexChild::class, inversedBy: 'subComplexChildren', )] #[JoinColumn( name: 'complex_type', referencedColumnName: 'complex_type', )] #[JoinColumn( name: 'complex_number', referencedColumnName: 'complex_number', )] #[JoinColumn( name: 'complexChild_point', referencedColumnName: 'point', )] protected ComplexChild $complexChild; ``` ? EDIT: Ok, this syntax was actually OK, but the code is using your PHPDoc syntax and with phpdoc, if you have multiple join columns, you need to wrap them with `ORM\JoinColumns`. I fixed this in my test
Author
Owner

@tomudding commented on GitHub (Dec 6, 2022):

It makes me wonder, is this even a valid syntax:

... (code not copied to reduce cutter)

Ok, this syntax was actually OK, but the code is using your PHPDoc syntax and with phpdoc, if you have multiple join columns, you need to wrap them with ORM\JoinColumns. I fixed this in my test

My apologies, we strictly use the attributes in our codebase and I could not be bothered to make my test case to work with that, so I simply copied them into their annotation form not accounting for the need of an @JoinColumns().

Thanks in advance for your time taking a look at this and trying to fix it!

@tomudding commented on GitHub (Dec 6, 2022): > It makes me wonder, is this even a valid syntax: > > ... (code not copied to reduce cutter) > > Ok, this syntax was actually OK, but the code is using your PHPDoc syntax and with phpdoc, if you have multiple join columns, you need to wrap them with ORM\JoinColumns. I fixed this in my test My apologies, we strictly use the attributes in our codebase and I could not be bothered to make my test case to work with that, so I simply copied them into their annotation form not accounting for the need of an `@JoinColumns()`. Thanks in advance for your time taking a look at this and trying to fix it!
Author
Owner

@michnovka commented on GitHub (Dec 6, 2022):

The title of this issue should be "Enum IDs cannot be part of association mapping"

@michnovka commented on GitHub (Dec 6, 2022): The title of this issue should be "Enum IDs cannot be part of association mapping"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7061