mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
ManyToMany IndexBy not working on multiple fields after upgrade to symfony 4.3 #6400
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @estebanolm on GitHub (Feb 13, 2020).
Bug Report
ManyToMany IndexBy not working on multiple fields after upgrade to symfony 4.3
Previously ther was no problem
Versions:
doctrine/annotations v1.8.0
doctrine/cache 1.10.0
doctrine/collections 1.6.4
doctrine/common 2.12.0
doctrine/dbal v2.10.1
doctrine/doctrine-bundle 2.0.7
doctrine/doctrine-migrations-bundle 2.1.2
doctrine/event-manager 1.1.0
doctrine/inflector 1.3.1
doctrine/instantiator 1.3.0
doctrine/lexer 1.2.0
doctrine/migrations 2.2.1
doctrine/orm v2.7.0
doctrine/persistence 1.3.6
doctrine/reflection v1.1.0
Summary
I have a many-to-many relation roles <-> user_roles <--> users
tables fields names (LOOK OUT THEY ARE DIFERENT):
[id_role] <---> [role_id, user_id] <--> [id]
with COMPOSITE PRIMARY index [user_id, role_id] (primary to be UNIQUE key), but it fails after upgrade
You can see more here: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/tutorials/working-with-indexed-associations.html
Reason to do that is:
When calling Collection::containsKey($key) on one-to-many and many-to-many collections using indexBy and EXTRA_LAZY a query is now executed to check for the existance for the item. Prevoiusly this operation was performed in memory by loading all entities of the collection.
From https://doctrine2.readthedocs.io/en/latest/changelog/migration_2_5.html
Current behavior
Error:
ClassMetadtaInfo.getAssociationMapping() try to find $this->associationMappings['role_id, user_id']
as it thins "'role_id, user_id'" is one fieldname
How to reproduce
if you remove indexBy="role_id, user_id", IT WORKS.
Before update, here was no problem.
i tried:
none worked
Expected behavior
Not to fail getting user entity
@SenseException commented on GitHub (Feb 13, 2020):
Please use a propper markdown format for your code examples to make it easier to read for those who join to help you.
I took a short look over your code examples and
mappedByshould point to a property, not a table column. I assume it's$roles.indexByor INDEX BY isn't something SQL related and AFAIK only supports one property.@beberlei commented on GitHub (Feb 13, 2020):
The indexBy must reference a property, not the columns. I am unsure how this has worked for you before, can you please post how you accessed it by these indexes before? So when doing $user->roles["..."] how did the lookup look like?
Edit: Also please post both ID declaration on User and Role entities.
@estebanolm commented on GitHub (Feb 14, 2020):
Sorry, it was my first or second question, and I didin't konw how to create markdown
This has worked for me since 4.2
In Migrations, was created, MAIN IMPORTANCE IS COMPOSED PRIMARY KEY:
@beberlei commented on GitHub (Feb 15, 2020):
I don‘t understand, you dont even use the roles collection as indexed by and you don‘t call containsKey() either. Instead you iterate over the collection and do a comparison by name/id. I believe you can just remove the indexBy instruction., unless you use this differently at another part in the code.
This might have worked before by accident, but it must be indexBy=„id_role“ using the htRole id property name.
@estebanolm commented on GitHub (Feb 24, 2020):
This code is from Symphony 2 after many upgrades. I don't remember, but I think I had problems becouse I used entities instead of strings, or there was extra calls to the database.
Indeed it worked, so yes, I think by accident.