-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
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:
request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\Mapping\MappingException: "No mapping found for field 'role_id, user_id' on class 'App\Entity\.....\Role'." at /....../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php line 163 {"exception":"[object] (Doctrine\\ORM\\Mapping\\MappingException(code: 0): No mapping found for field 'role_id, user_id' on class 'App\\Entity\\....\\Role'. at /...../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php:163)"} []
ClassMetadtaInfo.getAssociationMapping() try to find $this->associationMappings['role_id, user_id']
as it thins "'role_id, user_id'" is one fieldname
How to reproduce
CLASS ROLE
/**
* @ORM\ManyToMany(targetEntity="App\Entity\.....\User", mappedBy="id_role")
*/
protected $users;
CLASS USER
/**
* //indexBy="role_id, user_id",
*
* @ORM\ManyToMany(targetEntity="App\Entity\....\Role", **indexBy="role_id, user_id",** fetch="EXTRA_LAZY", inversedBy="users", cascade={"persist"})
* @ORM\JoinTable(name="sec_user_roles",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id_role", nullable=false)}
*/
protected $roles;
if you remove indexBy="role_id, user_id", IT WORKS.
Before update, here was no problem.
i tried:
indexBy="user_id, role_id"
indexBy="user_id"
none worked
Expected behavior
Not to fail getting user entity