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