Duplicate objects in collection of nested objects with multiple references #6945

Closed
opened 2026-01-22 15:41:57 +01:00 by admin · 1 comment
Owner

Originally created by @klammbueddel on GitHub (Mar 10, 2022).

Bug Report

Given 3 classes, a Container containing a list of Items and each item containing multiple Parts, if the container references both, the complete list of items ($container->items) and points to one item in the list ($container->currentItem), the parts of the item will be duplicated in the result set if fetched via DQL.

As a sidenote, the issue does not occur if the objects are already managed (see testIfAlreadyManaged in #9580).

Q A
BC Break no
Version 2.11.2

How to reproduce

$container = new Container();

# a container has items
$item = new Item();
$item->container = $container;
$container->items[]= $item;

# the container has a pointer on one item in the list of items
$container->currentItem = $item;

# an item has parts
$part = new Part();
$part->item = $item;
$item->parts[] = $part

$dql = <<<DQL
SELECT container, currentItem, currentItemPart, item, itemPart 
FROM Container container
LEFT JOIN container.currentItem currentItem
LEFT JOIN currentItem.parts currentItemPart  
LEFT JOIN container.items item 
LEFT JOIN item.parts itemPart
DQL;

# this test will fail
$containers = $this->_em->createQuery($dql)->execute();
self::assertCount(1, $containers);
self::assertCount(1, $containers[0]->items);
self::assertCount(1, $containers[0]->items[0]->parts); # << the item will contain 2 parts

Expected behavior

The $item->parts should not contain duplicates.

Originally created by @klammbueddel on GitHub (Mar 10, 2022). ### Bug Report Given 3 classes, a `Container` containing a list of `Item`s and each item containing multiple `Part`s, if the container references both, the complete list of items (`$container->items`) and points to one item in the list (`$container->currentItem`), the parts of the item will be duplicated in the result set if fetched via DQL. As a sidenote, the issue does not occur if the objects are already managed (see `testIfAlreadyManaged` in #9580). | Q | A |------------ | ------ | BC Break | no | Version | 2.11.2 #### How to reproduce ```php $container = new Container(); # a container has items $item = new Item(); $item->container = $container; $container->items[]= $item; # the container has a pointer on one item in the list of items $container->currentItem = $item; # an item has parts $part = new Part(); $part->item = $item; $item->parts[] = $part $dql = <<<DQL SELECT container, currentItem, currentItemPart, item, itemPart FROM Container container LEFT JOIN container.currentItem currentItem LEFT JOIN currentItem.parts currentItemPart LEFT JOIN container.items item LEFT JOIN item.parts itemPart DQL; # this test will fail $containers = $this->_em->createQuery($dql)->execute(); self::assertCount(1, $containers); self::assertCount(1, $containers[0]->items); self::assertCount(1, $containers[0]->items[0]->parts); # << the item will contain 2 parts ``` #### Expected behavior The `$item->parts` should not contain duplicates.
admin closed this issue 2026-01-22 15:41:57 +01:00
Author
Owner

@klammbueddel commented on GitHub (Mar 17, 2022):

Hi,
would be great if somebody could review the PR.
Thanks ;)

@klammbueddel commented on GitHub (Mar 17, 2022): Hi, would be great if somebody could review the PR. Thanks ;)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6945