Skip to content

Commit

Permalink
Skip joined entity creation for empty relation (#10889)
Browse files Browse the repository at this point in the history
  • Loading branch information
noemi-salaun committed Jan 28, 2024
1 parent 82533af commit 467f5c4
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,11 @@ protected function hydrateRowData(array $row, array &$result)
$parentObject = $this->resultPointers[$parentAlias];
} else {
// Parent object of relation not found, mark as not-fetched again
$element = $this->getEntity($data, $dqlAlias);
if (isset($nonemptyComponents[$dqlAlias])) {
$element = $this->getEntity($data, $dqlAlias);
} else {
$element = null;
}

// Update result pointer and provide initial fetch data for parent
$this->resultPointers[$dqlAlias] = $element;
Expand Down
108 changes: 108 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH10889Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
* @see https://github.com/doctrine/orm/issues/10889
*
* @group GH10889
*/
class GH10889Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->createSchemaForModels(
GH10889User::class,
GH10889Finger::class,
GH10889Hand::class
);
}

public function testIssue(): void
{
$user = new GH10889User();
$hand = new GH10889Hand($user, null);

$this->_em->persist($user);
$this->_em->persist($hand);
$this->_em->flush();
$this->_em->clear();

/** @var list<GH10889Hand> $hands */
$hands = $this->_em
->getRepository(GH10889Hand::class)
->createQueryBuilder('hand')
->leftJoin('hand.thumb', 'thumb')->addSelect('thumb')
->getQuery()
->getResult();

$this->assertArrayHasKey(0, $hands);
$this->assertEquals(1, $hands[0]->user->id);
$this->assertNull($hands[0]->thumb);
}
}

/**
* @ORM\Entity
*/
class GH10889User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*
* @var int
*/
public $id;
}

/**
* @ORM\Entity
*/
class GH10889Finger
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*
* @var int
*/
public $id;
}

/**
* @ORM\Entity
*/
class GH10889Hand
{
/**
* @ORM\Id
* @ORM\OneToOne(targetEntity="GH10889User")
* @ORM\GeneratedValue
*
* @var GH10889User
*/
public $user;

/**
* @ORM\ManyToOne(targetEntity="GH10889Finger")
*
* @var GH10889Finger|null
*/
public $thumb;

public function __construct(GH10889User $user, ?GH10889Finger $thumb)
{
$this->user = $user;
$this->thumb = $thumb;
}
}

0 comments on commit 467f5c4

Please sign in to comment.