DDC-3562: Make two different join request with different join condition with Doctrine ORM #4381

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

Originally created by @doctrinebot on GitHub (Feb 6, 2015).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user congelli501:

I want to make two requests on an object and join a one to many relation using doctrine ORM. The first request has different condition on the joined object.
The first request should join one object, and the second one two, but both return one object when run with the same EntityManager context.

The test entity

    /****
     * TreeNode
     *
     * @ORM\Table(name="treeNode")
     */
    class TreeNode {
        /****
        * @var integer
        *
        * @ORM\Id
        * @ORM\Column(name="id", type="integer")
        * @ORM\GeneratedValue(strategy="AUTO")
        */
        private $id;

        /****
        * @var string
        *
        * @ORM\Column(name="name", type="string", length=50)
        */
        private $name;

        /****
        * @var \Doctrine\Common\Collections\Collection
        *
        * @ORM\JoinColumn(name="subdirs", nullable=true)
        * @ORM\OneToMany(targetEntity="TestBundle\Entity\TreeNode", mappedBy="parent", cascade={"persist"})
        */
        private $subdirs;

        /****
        * @var TestBundle\Entity\TreeNode
        *
        * @ORM\JoinColumn(name="parent", nullable=true)
        * @ORM\ManyToOne(targetEntity="TestBundle\Entity\TreeNode", inversedBy="subdirs")
        */
        private $parent;
    }

Adding some test data

    $parent = new TreeNode();
    $parent->setName('Parent');
    $parent->setParent(null);

    $child1 = new TreeNode();
    $child1->setName('Child1');
    $child1->setParent($parent);

    $child2 = new TreeNode();
    $child2->setName('Child2');
    $child2->setParent($parent);

Run the queries

    $repo = $em->getRepository('TestBundle:TreeNode');

    $qb = $repo->createQueryBuilder('t');
    $qb->innerJoin('t.subdirs', 'subnode')
            ->addSelect('subnode')
            ->where('t.name = :parentName')
            ->setParameter('childName', 'Child1')
            ->andWhere('subnode.name = :childName')
            ->setParameter('parentName', 'Parent');

    $result = $qb->getQuery()->getOneOrNullResult();
    var_dump($result->getSubdirs()->count()); // Gives 1, ok 


    // Get parent parent + all childs
    $qb = $repo->createQueryBuilder('t');
    $qb->innerJoin('t.subdirs', 'subnode')
            ->addSelect('subnode')
            ->where('t.name = :parentName')
            ->setParameter('parentName', 'Parent');
    $result = $qb->getQuery()->getOneOrNullResult();
    var_dump($result->getSubdirs()->count()); // Gives 1, should have been 2 !
  • The second request only returns one "subdir" object, while it should have been two.
  • If I run the second request without the first one, I get the two results, as expected. Inverting the request makes the second one return two results instead of one.
  • The correct SQL requests are run by this code, only the returned result is wrong.
  • Running $em->clear() between the two statements works, but I don't want to untrack my other entities.
Originally created by @doctrinebot on GitHub (Feb 6, 2015). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user congelli501: I want to make two requests on an object and join a one to many relation using doctrine ORM. The first request has different condition on the joined object. The first request should join one object, and the second one two, but both return one object when run with the same EntityManager context. # The test entity ``` /**** * TreeNode * * @ORM\Table(name="treeNode") */ class TreeNode { /**** * @var integer * * @ORM\Id * @ORM\Column(name="id", type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /**** * @var string * * @ORM\Column(name="name", type="string", length=50) */ private $name; /**** * @var \Doctrine\Common\Collections\Collection * * @ORM\JoinColumn(name="subdirs", nullable=true) * @ORM\OneToMany(targetEntity="TestBundle\Entity\TreeNode", mappedBy="parent", cascade={"persist"}) */ private $subdirs; /**** * @var TestBundle\Entity\TreeNode * * @ORM\JoinColumn(name="parent", nullable=true) * @ORM\ManyToOne(targetEntity="TestBundle\Entity\TreeNode", inversedBy="subdirs") */ private $parent; } ``` # Adding some test data ``` $parent = new TreeNode(); $parent->setName('Parent'); $parent->setParent(null); $child1 = new TreeNode(); $child1->setName('Child1'); $child1->setParent($parent); $child2 = new TreeNode(); $child2->setName('Child2'); $child2->setParent($parent); ``` # Run the queries ``` $repo = $em->getRepository('TestBundle:TreeNode'); $qb = $repo->createQueryBuilder('t'); $qb->innerJoin('t.subdirs', 'subnode') ->addSelect('subnode') ->where('t.name = :parentName') ->setParameter('childName', 'Child1') ->andWhere('subnode.name = :childName') ->setParameter('parentName', 'Parent'); $result = $qb->getQuery()->getOneOrNullResult(); var_dump($result->getSubdirs()->count()); // Gives 1, ok // Get parent parent + all childs $qb = $repo->createQueryBuilder('t'); $qb->innerJoin('t.subdirs', 'subnode') ->addSelect('subnode') ->where('t.name = :parentName') ->setParameter('parentName', 'Parent'); $result = $qb->getQuery()->getOneOrNullResult(); var_dump($result->getSubdirs()->count()); // Gives 1, should have been 2 ! ``` - The second request only returns one "subdir" object, while it should have been two. - If I run the second request without the first one, I get the two results, as expected. Inverting the request makes the second one return two results instead of one. - The correct SQL requests are run by this code, only the returned result is wrong. - Running `$em->clear()` between the two statements works, but I don't want to untrack my other entities.
admin added the Bug label 2026-01-22 14:40:36 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4381