Selecting the same alias twice results in "The given entity has no identity/no id values set. It cannot be added to the identity map." #7270

Closed
opened 2026-01-22 15:48:37 +01:00 by admin · 3 comments
Owner

Originally created by @isarastov on GitHub (Dec 1, 2023).

BC Break Report

Q A
BC Break yes
Version 2.16.0

EDIT

#11194 seems to resolve this issue.
My original assumption was that it's caused by the alias being selected twice, but this proved wrong. It's more probably caused by something in the result itself (at least this seems to be the case for me and @winiarekk)

Summary

After upgrading to 2.16.0 a query that was selecting the same alias twice started throwing the following exception:

The given entity of type 'Baz' (Baz@31833) has no identity/no id values set. It cannot be added to the identity map.

Obviously the query shouldn't select the same alias twice, however this was working until 2.15.5.
I was able to identify the change that causes the different behavior as this one. If I add the 4 lines from 2.15.5 instead of the call to registerManaged it works but I noticed that the $idHash is an empty string in this case.
I also found this issue that seems related: #10889

Previous behavior

The Foo entity was hydrated successfully.

Current behavior

Doctrine\ORM\ORMInvalidArgumentException:

The given entity of type 'Baz' (Baz@31833) has no identity/no id values set. It cannot be added to the identity map.

in doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php -> getIdHashByEntity

How to reproduce

I tried to reproduce this in a test case, but with no success. Anyway, here's a simplified version of the code that causes the error:

<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;

class GH11100Test extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->createSchemaForModels(
            GH11100Foo::class,
            GH11100Bar::class,
            GH11100Baz::class
        );
    }

    public function testSelectingTheSameAliasTwiceShouldWork(): void
    {
        $foo = new GH11100Foo();
        $bar = new GH11100Bar();
        $baz = new GH11100Baz();
        $foo->addBar($bar);
        $bar->addBaz($baz);
        $this->_em->persist($foo);
        $this->_em->persist($bar);
        $this->_em->persist($baz);
        $this->_em->flush();
        $this->_em->clear();

        $fooRepo = $this->_em->getRepository(GH11100Foo::class);
        $fooRepo->createQueryBuilder('f')
            ->select('f, fb, fbbz, fbbz')
            ->leftJoin('f.bar', 'fb')
            ->leftJoin('fb.baz', 'fbbz')
            ->getQuery()->getOneOrNullResult();
    }
}

/**
 * @ORM\Entity
 */
class GH11100Foo
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Column(type="integer")
     *
     * @var int
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="GH11100Bar", mappedBy="foo")
     */
    private $bar;

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

    public function addBar($bar) {
        $this->bar[] = $bar;
    }
}

/**
 * @ORM\Entity
 */
class GH11100Bar
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="GH11100Foo", inversedBy="bar")
     */
    private $foo;

    /**
     * @ORM\OneToMany(targetEntity="GH11100Baz", mappedBy="bar")
     */
    private $baz;

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

    public function getFoo()
    {
        return $this->foo;
    }

    public function setFoo($foo)
    {
        $this->foo = $foo;

        return $this;
    }

    public function addBaz($baz)
    {
        $this->baz[] = $baz;
    }
}

/**
 * @ORM\Entity
 */
class GH11100Baz
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="GH11100Bar", inversedBy="baz")
     */
    private $bar;

    public function getBar()
    {
        return $this->bar;
    }

    public function setBar($bar)
    {
        $this->bar = $bar;

        return $this;
    }
}
Originally created by @isarastov on GitHub (Dec 1, 2023). ### BC Break Report | Q | A |------------ | ------ | BC Break | yes | Version | 2.16.0 #### EDIT #11194 seems to resolve this issue. My original assumption was that it's caused by the alias being selected twice, but this proved wrong. It's more probably caused by something in the result itself (at least this seems to be the case for me and @winiarekk) #### Summary After upgrading to 2.16.0 a query that was selecting the same alias twice started throwing the following exception: > The given entity of type 'Baz' (Baz@31833) has no identity/no id values set. It cannot be added to the identity map. Obviously the query shouldn't select the same alias twice, however this was working until 2.15.5. I was able to identify the change that causes the different behavior as [this one](https://github.com/doctrine/orm/commit/01a14327d2404885897961851659b467ec966d74#diff-55a900494fc8033ab498c53929716caf0aa39d6bdd7058e7d256787a24412ee4L2817-R2845). If I add the 4 lines from 2.15.5 instead of the call to `registerManaged` it works but I noticed that the `$idHash` is an empty string in this case. I also found this issue that seems related: #10889 #### Previous behavior The Foo entity was hydrated successfully. #### Current behavior Doctrine\ORM\ORMInvalidArgumentException: > The given entity of type 'Baz' (Baz@31833) has no identity/no id values set. It cannot be added to the identity map. in doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php -> getIdHashByEntity #### How to reproduce I tried to reproduce this in a test case, but with no success. Anyway, here's a simplified version of the code that causes the error: ```php <?php declare(strict_types=1); namespace Doctrine\Tests\ORM\Functional\Ticket; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use Doctrine\Tests\OrmFunctionalTestCase; class GH11100Test extends OrmFunctionalTestCase { protected function setUp(): void { parent::setUp(); $this->createSchemaForModels( GH11100Foo::class, GH11100Bar::class, GH11100Baz::class ); } public function testSelectingTheSameAliasTwiceShouldWork(): void { $foo = new GH11100Foo(); $bar = new GH11100Bar(); $baz = new GH11100Baz(); $foo->addBar($bar); $bar->addBaz($baz); $this->_em->persist($foo); $this->_em->persist($bar); $this->_em->persist($baz); $this->_em->flush(); $this->_em->clear(); $fooRepo = $this->_em->getRepository(GH11100Foo::class); $fooRepo->createQueryBuilder('f') ->select('f, fb, fbbz, fbbz') ->leftJoin('f.bar', 'fb') ->leftJoin('fb.baz', 'fbbz') ->getQuery()->getOneOrNullResult(); } } /** * @ORM\Entity */ class GH11100Foo { /** * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") * @ORM\Column(type="integer") * * @var int */ private $id; /** * @ORM\OneToMany(targetEntity="GH11100Bar", mappedBy="foo") */ private $bar; public function __construct() { $this->bar = new ArrayCollection(); } public function addBar($bar) { $this->bar[] = $bar; } } /** * @ORM\Entity */ class GH11100Bar { /** * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") * @ORM\Column(type="integer") */ private $id; /** * @ORM\ManyToOne(targetEntity="GH11100Foo", inversedBy="bar") */ private $foo; /** * @ORM\OneToMany(targetEntity="GH11100Baz", mappedBy="bar") */ private $baz; public function __construct() { $this->baz = new ArrayCollection(); } public function getFoo() { return $this->foo; } public function setFoo($foo) { $this->foo = $foo; return $this; } public function addBaz($baz) { $this->baz[] = $baz; } } /** * @ORM\Entity */ class GH11100Baz { /** * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") * @ORM\Column(type="integer") */ private $id; /** * @ORM\ManyToOne(targetEntity="GH11100Bar", inversedBy="baz") */ private $bar; public function getBar() { return $this->bar; } public function setBar($bar) { $this->bar = $bar; return $this; } } ```
admin closed this issue 2026-01-22 15:48:38 +01:00
Author
Owner

@winiarekk commented on GitHub (Mar 1, 2024):

I have the same issue after upgrading doctrine, however in my case there is definitely no duplicated select of the same alias. What's even more interesting, the hydration for the query works good if I change one parameter in it, so it seems to be somehow related to the data.

I was trying to figure out what's wrong, so I inspected the raw results of SQL between non-working and working queries. The only difference, I was able to observe, is that the failing query has only one row in the result, when the successful ones return at least few of them, but no idea if this is related.

EDIT:
This PR resolves my problem https://github.com/doctrine/orm/pull/11194

@winiarekk commented on GitHub (Mar 1, 2024): I have the same issue after upgrading doctrine, however in my case there is definitely no duplicated select of the same alias. What's even more interesting, the hydration for the query works good if I change one parameter in it, so it seems to be somehow related to the data. I was trying to figure out what's wrong, so I inspected the raw results of SQL between non-working and working queries. The only difference, I was able to observe, is that the failing query has only one row in the result, when the successful ones return at least few of them, but no idea if this is related. EDIT: This PR resolves my problem https://github.com/doctrine/orm/pull/11194
Author
Owner

@isarastov commented on GitHub (Mar 1, 2024):

#11194 solves the issue for me too.
I also tested two cases without the fix - the first one returns 4 rows (for the same Foo) and it works, the other returns 39 rows (again, for the same Foo) and it fails. So this probably has nothing to do with the multiple selects.

@isarastov commented on GitHub (Mar 1, 2024): #11194 solves the issue for me too. I also tested two cases without the fix - the first one returns 4 rows (for the same Foo) and it works, the other returns 39 rows (again, for the same Foo) and it fails. So this probably has nothing to do with the multiple selects.
Author
Owner

@alexander-schranz commented on GitHub (Jun 27, 2024):

https://github.com/doctrine/orm/pull/11194 is merged so this may also be resolved?

@alexander-schranz commented on GitHub (Jun 27, 2024): https://github.com/doctrine/orm/pull/11194 is merged so this may also be resolved?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7270