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

Verify UnitOfWork::HINT_DEFEREAGERLOAD exists and is true #10808

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -2976,7 +2976,9 @@ public function createEntity($className, array $data, &$hints = [])
break;

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

Expand Down
107 changes: 107 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH10808Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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 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
WITH appointment_child.id = 1'
);

// 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", 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;
}