Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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." #11100

Closed
isarastov opened this issue Dec 1, 2023 · 3 comments

Comments

@isarastov
Copy link

isarastov commented 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;
    }
}
@winiarekk
Copy link

winiarekk commented 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 #11194

@isarastov
Copy link
Author

#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.

@alexander-schranz
Copy link
Contributor

#11194 is merged so this may also be resolved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants