PHP 7.4: Object Hydrator is trying to access related entity without initialization when fetch mode = "EAGER" #6364

Closed
opened 2026-01-22 15:31:50 +01:00 by admin · 4 comments
Owner

Originally created by @EvgeniiR on GitHub (Dec 6, 2019).

Bug Report

Q A
BC Break no
Version 2.7.0

Summary

Error when calling \Doctrine\ORM\EntityManager::find:

[Error] Typed property App\TestEnt::$relEnt must not be accessed before initialization  
                                                                                                             
#1  /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:432
#2  /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:162
#3  /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:153
#4  /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:726
#5  /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:736
#6  /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:469

How to reproduce

Entities
<?php

declare(strict_types=1);

namespace App\test;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class TestEnt
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="guid")
     */
    private string $id;

    /**
     * @ORM\OneToOne(targetEntity="TestRelEnt", fetch="EAGER", cascade={"persist"})
     */
    private TestRelEnt $relEnt;

    public function __construct(string $id, TestRelEnt $relEnt)
    {
        $this->id = $id;
        $this->relEnt = $relEnt;
    }
}
<?php

declare(strict_types=1);

namespace App\test;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class TestRelEnt
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue()
     */
    private string $id;
}
Code
$id = Uuid::uuid4()->toString();
$testEnt = new TestEnt($id, new TestRelEnt());
$this->entityManager->persist($testEnt);
$this->entityManager->flush();
$this->entityManager->clear();
$this->entityManager->find(TestEnt::class, $id);
Originally created by @EvgeniiR on GitHub (Dec 6, 2019). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | no | Version | 2.7.0 #### Summary Error when calling ```\Doctrine\ORM\EntityManager::find```: ``` [Error] Typed property App\TestEnt::$relEnt must not be accessed before initialization #1 /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:432 #2 /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:162 #3 /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:153 #4 /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:726 #5 /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:736 #6 /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:469 ``` #### How to reproduce <details> <summary>Entities</summary> ```php <?php declare(strict_types=1); namespace App\test; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity() */ class TestEnt { /** * @ORM\Id * @ORM\Column(name="id", type="guid") */ private string $id; /** * @ORM\OneToOne(targetEntity="TestRelEnt", fetch="EAGER", cascade={"persist"}) */ private TestRelEnt $relEnt; public function __construct(string $id, TestRelEnt $relEnt) { $this->id = $id; $this->relEnt = $relEnt; } } ``` ```php <?php declare(strict_types=1); namespace App\test; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity() */ class TestRelEnt { /** * @ORM\Id * @ORM\Column(name="id", type="integer") * @ORM\GeneratedValue() */ private string $id; } ``` </details> <details> <summary>Code</summary> ```php $id = Uuid::uuid4()->toString(); $testEnt = new TestEnt($id, new TestRelEnt()); $this->entityManager->persist($testEnt); $this->entityManager->flush(); $this->entityManager->clear(); $this->entityManager->find(TestEnt::class, $id); ``` </details>
admin added the Bug label 2026-01-22 15:31:50 +01:00
admin closed this issue 2026-01-22 15:31:51 +01:00
Author
Owner

@oojacoboo commented on GitHub (Jan 7, 2020):

There are 2 pull requests that have attempted to address this:

https://github.com/doctrine/orm/pull/7950 and https://github.com/doctrine/orm/pull/7857

@oojacoboo commented on GitHub (Jan 7, 2020): There are 2 pull requests that have attempted to address this: https://github.com/doctrine/orm/pull/7950 and https://github.com/doctrine/orm/pull/7857
Author
Owner

@SenseException commented on GitHub (Jan 13, 2020):

@EvgeniiR Can we close this issue in favor of #7854? It seems to be a duplicate.

@SenseException commented on GitHub (Jan 13, 2020): @EvgeniiR Can we close this issue in favor of #7854? It seems to be a duplicate.
Author
Owner

@EvgeniiR commented on GitHub (Jan 14, 2020):

@SenseException Yes, seems that https://github.com/doctrine/orm/pull/7857 have test case for this.

@EvgeniiR commented on GitHub (Jan 14, 2020): @SenseException Yes, seems that https://github.com/doctrine/orm/pull/7857 have test case for this.
Author
Owner

@SenseException commented on GitHub (Jan 14, 2020):

Thank you 👍

@SenseException commented on GitHub (Jan 14, 2020): Thank you :+1:
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6364