Skip to content

ManyToMany IndexBy not working on multiple fields after upgrade to symfony 4.3 #8018

@estebanolm

Description

@estebanolm

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy