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

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

Closed
johannes85 opened this issue Oct 14, 2022 · 10 comments · Fixed by #10274
Closed

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

johannes85 opened this issue Oct 14, 2022 · 10 comments · Fixed by #10274

Comments

@johannes85
Copy link

johannes85 commented 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.

@beberlei
Copy link
Member

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

@johannes85
Copy link
Author

There is no way to implement toString for an enum.

@beberlei
Copy link
Member

I did not know / forgot this, hmmm

@michnovka
Copy link
Contributor

Why isnt identifier flattener used in this case?

Check #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.

@flight643
Copy link

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 #10158 (comment) was taken into account

@michnovka
Copy link
Contributor

@flight643 please provide broken test case

@tomudding
Copy link

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.

@michnovka
Copy link
Contributor

michnovka commented 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

@tomudding
Copy link

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!

@michnovka
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants