Partially joined toMany-relation is set as full collection #6659

Open
opened 2026-01-22 15:36:34 +01:00 by admin · 0 comments
Owner

Originally created by @villermen on GitHub (Mar 24, 2021).

Consider the following basic entity relation:

/**
 * @ORM\Entity
 */
class Foo
{
    /**
     * @ORM\OneToMany(targetEntity=Bar::class, mappedBy="foo")
     */
    private Collection $bars;

    public function __construct()
    {
        $this->bars = new ArrayCollection();
    }
}

/**
 * @ORM\Entity
 */
class Bar
{
    /**
     * @ORM\ManyToOne(targetEntity=Foo::class, inversedBy="bars")
     */
    private Foo $foo;

    /**
     * @ORM\Column(type="integer")
     */
    private int $option = 0;
}

When you select a filtered portion of the "bars" relation:

SELECT f, b FROM Foo f LEFT JOIN f.bars b WHERE b.option = 1

You will end up with a hydrated Foo::$bars property that contains only the bars where Bar::$option = 1. Even when the database contains entities for foo with other values for Bar::$option.

I would expect hydration of Foo::$bars to trigger an additional query to retrieve the remaining entities in the relation, because the collection has not been fully loaded yet.

Originally created by @villermen on GitHub (Mar 24, 2021). Consider the following basic entity relation: ```php /** * @ORM\Entity */ class Foo { /** * @ORM\OneToMany(targetEntity=Bar::class, mappedBy="foo") */ private Collection $bars; public function __construct() { $this->bars = new ArrayCollection(); } } /** * @ORM\Entity */ class Bar { /** * @ORM\ManyToOne(targetEntity=Foo::class, inversedBy="bars") */ private Foo $foo; /** * @ORM\Column(type="integer") */ private int $option = 0; } ``` When you select a _filtered portion_ of the "bars" relation: ```sql SELECT f, b FROM Foo f LEFT JOIN f.bars b WHERE b.option = 1 ``` You will end up with a hydrated `Foo::$bars` property that contains only the bars where `Bar::$option = 1`. Even when the database contains entities for foo with other values for `Bar::$option`. I would expect hydration of `Foo::$bars` to trigger an additional query to retrieve the remaining entities in the relation, because the collection has not been fully loaded yet.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6659