Skip to content

Commit a60a273

Browse files
authored
Merge pull request #10808 from oscmarb/verifiy-hint-defer-eager-load-is-true
Verify UnitOfWork::HINT_DEFEREAGERLOAD exists and is true
2 parents 17500f5 + 7986fc6 commit a60a273

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

lib/Doctrine/ORM/UnitOfWork.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -3058,7 +3058,9 @@ public function createEntity($className, array $data, &$hints = [])
30583058
break;
30593059

30603060
// Deferred eager load only works for single identifier classes
3061-
case isset($hints[self::HINT_DEFEREAGERLOAD]) && ! $targetClass->isIdentifierComposite:
3061+
case isset($hints[self::HINT_DEFEREAGERLOAD]) &&
3062+
$hints[self::HINT_DEFEREAGERLOAD] &&
3063+
! $targetClass->isIdentifierComposite:
30623064
// TODO: Is there a faster approach?
30633065
$this->eagerLoadingEntities[$targetClass->rootEntityName][$relatedIdHash] = current($normalizedAssociatedId);
30643066

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional\Ticket;
6+
7+
use Doctrine\ORM\Mapping\Column;
8+
use Doctrine\ORM\Mapping\Entity;
9+
use Doctrine\ORM\Mapping\GeneratedValue;
10+
use Doctrine\ORM\Mapping\Id;
11+
use Doctrine\ORM\Mapping\JoinColumn;
12+
use Doctrine\ORM\Mapping\OneToOne;
13+
use Doctrine\ORM\Mapping\Table;
14+
use Doctrine\ORM\UnitOfWork;
15+
use Doctrine\Tests\OrmFunctionalTestCase;
16+
17+
use function get_class;
18+
19+
/** @group GH10808 */
20+
class GH10808Test extends OrmFunctionalTestCase
21+
{
22+
protected function setUp(): void
23+
{
24+
parent::setUp();
25+
26+
$this->createSchemaForModels(
27+
GH10808Appointment::class,
28+
GH10808AppointmentChild::class
29+
);
30+
}
31+
32+
public function testDQLDeferredEagerLoad(): void
33+
{
34+
$appointment = new GH10808Appointment();
35+
36+
$this->_em->persist($appointment);
37+
$this->_em->flush();
38+
$this->_em->clear();
39+
40+
$query = $this->_em->createQuery(
41+
'SELECT appointment from Doctrine\Tests\ORM\Functional\Ticket\GH10808Appointment appointment
42+
JOIN appointment.child appointment_child
43+
WITH appointment_child.id = 1'
44+
);
45+
46+
// By default, UnitOfWork::HINT_DEFEREAGERLOAD is set to 'true'
47+
$deferredLoadResult = $query->getSingleResult();
48+
49+
// Clear the EM to prevent the recovery of the loaded instance, which would otherwise result in a proxy.
50+
$this->_em->clear();
51+
52+
$eagerLoadResult = $query->setHint(UnitOfWork::HINT_DEFEREAGERLOAD, false)->getSingleResult();
53+
54+
self::assertNotEquals(
55+
GH10808AppointmentChild::class,
56+
get_class($deferredLoadResult->child),
57+
'$deferredLoadResult->child should be a proxy'
58+
);
59+
self::assertEquals(
60+
GH10808AppointmentChild::class,
61+
get_class($eagerLoadResult->child),
62+
'$eagerLoadResult->child should not be a proxy'
63+
);
64+
}
65+
}
66+
67+
/**
68+
* @Entity
69+
* @Table(name="gh10808_appointment")
70+
*/
71+
class GH10808Appointment
72+
{
73+
/**
74+
* @var int
75+
* @Id
76+
* @Column(type="integer")
77+
* @GeneratedValue
78+
*/
79+
public $id;
80+
81+
/**
82+
* @var GH10808AppointmentChild
83+
* @OneToOne(targetEntity="GH10808AppointmentChild", cascade={"persist", "remove"}, fetch="EAGER")
84+
* @JoinColumn(name="child_id", referencedColumnName="id")
85+
*/
86+
public $child;
87+
88+
public function __construct()
89+
{
90+
$this->child = new GH10808AppointmentChild();
91+
}
92+
}
93+
94+
/**
95+
* @Entity
96+
* @Table(name="gh10808_appointment_child")
97+
*/
98+
class GH10808AppointmentChild
99+
{
100+
/**
101+
* @var int
102+
* @Id
103+
* @Column(type="integer")
104+
* @GeneratedValue
105+
*/
106+
private $id;
107+
}

0 commit comments

Comments
 (0)