Skip to content

Commit 3a32c00

Browse files
m-r-rgreg0ire
authored andcommitted
Add a failing test for issue #7505
1 parent adfd010 commit 3a32c00

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ protected function hydrateAllData()
7878
*/
7979
protected function hydrateRowData(array $sqlResult, array &$result)
8080
{
81-
$entityName = $this->class->name;
82-
$data = [];
81+
$entityName = $this->class->name;
82+
$data = [];
83+
$discrColumnValue = null;
8384

8485
// We need to find the correct entity class name if we have inheritance in resultset
8586
if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
@@ -104,7 +105,8 @@ protected function hydrateRowData(array $sqlResult, array &$result)
104105
throw HydrationException::invalidDiscriminatorValue($sqlResult[$discrColumnName], array_keys($discrMap));
105106
}
106107

107-
$entityName = $discrMap[$sqlResult[$discrColumnName]];
108+
$entityName = $discrMap[$sqlResult[$discrColumnName]];
109+
$discrColumnValue = $sqlResult[$discrColumnName];
108110

109111
unset($sqlResult[$discrColumnName]);
110112
}
@@ -134,6 +136,11 @@ protected function hydrateRowData(array $sqlResult, array &$result)
134136

135137
// Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator)
136138
if ( ! isset($data[$fieldName]) || ! $valueIsNull) {
139+
// If we have inheritance in resultset, make sure the field belongs to the correct class
140+
if (isset($cacheKeyInfo['discriminatorValues']) && ! in_array($discrColumnValue, $cacheKeyInfo['discriminatorValues'], true)) {
141+
continue;
142+
}
143+
137144
$data[$fieldName] = $value;
138145
}
139146
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional\Ticket;
6+
7+
use Doctrine\ORM\Annotation as ORM;
8+
use Doctrine\Tests\OrmFunctionalTestCase;
9+
10+
final class GH7505Test extends OrmFunctionalTestCase
11+
{
12+
/**
13+
* {@inheritDoc}
14+
*/
15+
protected function setUp() : void
16+
{
17+
parent::setUp();
18+
19+
$this->setUpEntitySchema([
20+
GH7505AbstractResponse::class,
21+
GH7505ArrayResponse::class,
22+
GH7505TextResponse::class,
23+
]);
24+
}
25+
26+
public function testSimpleArrayTypeHydratedCorrectly() : void
27+
{
28+
$arrayResponse = new GH7505ArrayResponse();
29+
$this->em->persist($arrayResponse);
30+
31+
$textResponse = new GH7505TextResponse();
32+
$this->em->persist($textResponse);
33+
34+
$this->em->flush();
35+
$this->em->clear();
36+
37+
$repository = $this->em->getRepository(GH7505AbstractResponse::class);
38+
39+
/** @var GH7505ArrayResponse $arrayResponse */
40+
$arrayResponse = $repository->find($arrayResponse->id);
41+
self::assertSame([], $arrayResponse->value);
42+
43+
/** @var GH7505TextResponse $textResponse */
44+
$textResponse = $repository->find($textResponse->id);
45+
self::assertNull($textResponse->value);
46+
}
47+
}
48+
49+
/**
50+
* @ORM\Entity()
51+
* @ORM\Table(name="gh7505_responses")
52+
* @ORM\InheritanceType("SINGLE_TABLE")
53+
* @ORM\DiscriminatorColumn(name="discr", type="string")
54+
* @ORM\DiscriminatorMap({
55+
* "array" = GH7505ArrayResponse::class,
56+
* "text" = GH7505TextResponse::class,
57+
* })
58+
*/
59+
abstract class GH7505AbstractResponse
60+
{
61+
/**
62+
* @ORM\Id @ORM\GeneratedValue
63+
* @ORM\Column(type="integer")
64+
*/
65+
public $id;
66+
}
67+
68+
/**
69+
* @ORM\Entity()
70+
*/
71+
class GH7505ArrayResponse extends GH7505AbstractResponse
72+
{
73+
/**
74+
* @ORM\Column(name="value_array", type="simple_array")
75+
*
76+
* @var array
77+
*/
78+
public $value = [];
79+
}
80+
81+
/**
82+
* @ORM\Entity()
83+
*/
84+
class GH7505TextResponse extends GH7505AbstractResponse
85+
{
86+
/**
87+
* @ORM\Column(name="value_string", type="string")
88+
*
89+
* @var string|null
90+
*/
91+
public $value;
92+
}

0 commit comments

Comments
 (0)