mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
ArrayCollection of root entity not filled with related entities. #7486
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 @AndreRudolph on GitHub (Mar 10, 2025).
Bug Report
Many To Many by using Pivot entity as One To Many <-> Many To One does not fill ArrayCollection with entities.
Summary
I have a N:M relation which I split up into One To Many <-> Many to One by adding a pivot entity. More precisely I have the pivot Entity TeamUsers which references both sides, the Team and the User.
Current behavior
I have these entities:
`#[Entity]
#[Table(name: 'teams')]
#[HasLifecycleCallbacks]
class Team implements TeamInterface {
}`
`
#[Entity]
#[Table(name: 'team_users')]
#[HasLifecycleCallbacks]
class TeamUser implements TeamUserInterface {
}`
`
#[Entity]
#[Table(name: 'users')]
#[HasLifecycleCallbacks]
class User implements UserInterface
{
#[Id]
#[GeneratedValue(strategy: 'CUSTOM')]
#[CustomIdGenerator(class: UuidGenerator::class)]
#[Column(type: 'string', length: 36, unique: true)]
private string $id;
}`
When I run this following query
SELECT team, teamUser FROM App\Infrastructure\Entities\Team team LEFT JOIN team.teamUsers teamUserThen I get the root entity (team) but with an empty TeamUser collection. I ran the actual SQL query and it returns the expected results and after some debugging I figured out that doctrine correctly creates the entities (Team, TeamUser) but it does not link them, so somehow it does not add the TeamUser entries to the ArrayCollection of the root entity.
Am I doing something wrong with the mapping or do I oversee something?
PS: The doctrine command for schema validation also gives feedback, that the mapping files are correct.
@AndreRudolph commented on GitHub (Mar 10, 2025):
After some more debugging I figured out that the issue was that the already created (and thus managed) entities did have not the same state as the retrieved ones by the query. A call
$em->clear()worked and revealed that there was a stale state of the original entities.