Skip to content

Commit 1767f4b

Browse files
committed
Merge branch '2.7' into 2.8.x
2 parents ca95b0e + 401db45 commit 1767f4b

File tree

3 files changed

+104
-4
lines changed

3 files changed

+104
-4
lines changed

docs/en/reference/annotations-reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Optional attributes:
9999

100100
- **length**: Used by the "string" type to determine its maximum
101101
length in the database. Doctrine does not validate the length of a
102-
string values for you.
102+
string value for you.
103103

104104
- **precision**: The precision for a decimal (exact numeric) column
105105
(applies only for decimal column), which is the maximum number of

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PDO;
2323
use Doctrine\ORM\Mapping\ClassMetadata;
2424
use Doctrine\ORM\Query;
25+
use function in_array;
2526

2627
class SimpleObjectHydrator extends AbstractHydrator
2728
{
@@ -78,8 +79,9 @@ protected function hydrateAllData()
7879
*/
7980
protected function hydrateRowData(array $sqlResult, array &$result)
8081
{
81-
$entityName = $this->class->name;
82-
$data = [];
82+
$entityName = $this->class->name;
83+
$data = [];
84+
$discrColumnValue = null;
8385

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

107-
$entityName = $discrMap[$sqlResult[$discrColumnName]];
109+
$entityName = $discrMap[$sqlResult[$discrColumnName]];
110+
$discrColumnValue = $sqlResult[$discrColumnName];
108111

109112
unset($sqlResult[$discrColumnName]);
110113
}
@@ -134,6 +137,11 @@ protected function hydrateRowData(array $sqlResult, array &$result)
134137

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

0 commit comments

Comments
 (0)