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

Fix enum hydration when fetching partial results #9657

Merged
merged 7 commits into from
Apr 16, 2022
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
17 changes: 17 additions & 0 deletions lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ORM\Internal\Hydration;

use BackedEnum;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\ForwardCompatibility\Result as ForwardCompatibilityResult;
use Doctrine\DBAL\Platforms\AbstractPlatform;
Expand All @@ -27,6 +28,7 @@
use function end;
use function get_debug_type;
use function in_array;
use function is_array;
use function sprintf;

/**
Expand Down Expand Up @@ -429,7 +431,20 @@ protected function gatherRowData(array $data, array &$id, array &$nonemptyCompon
$type = $cacheKeyInfo['type'];
$value = $type->convertToPHPValue($value, $this->_platform);

// Reimplement ReflectionEnumProperty code
if ($value !== null && isset($cacheKeyInfo['enumType'])) {
$enumType = $cacheKeyInfo['enumType'];
if (is_array($value)) {
$value = array_map(static function ($value) use ($enumType): BackedEnum {
return $enumType::from($value);
}, $value);
} else {
$value = $enumType::from($value);
}
}

$rowData['scalars'][$fieldName] = $value;

break;

//case (isset($cacheKeyInfo['isMetaColumn'])):
Expand Down Expand Up @@ -572,13 +587,15 @@ protected function hydrateColumnInfo($key)
'fieldName' => $this->_rsm->scalarMappings[$key],
'type' => Type::getType($this->_rsm->typeMappings[$key]),
'dqlAlias' => '',
'enumType' => $this->_rsm->enumMappings[$key] ?? null,
];

case isset($this->_rsm->scalarMappings[$key]):
return $this->_cache[$key] = [
'isScalar' => true,
'fieldName' => $this->_rsm->scalarMappings[$key],
'type' => Type::getType($this->_rsm->typeMappings[$key]),
'enumType' => $this->_rsm->enumMappings[$key] ?? null,
];

case isset($this->_rsm->metaMappings[$key]):
Expand Down
23 changes: 23 additions & 0 deletions lib/Doctrine/ORM/Query/ResultSetMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ class ResultSetMapping
*/
public $scalarMappings = [];

/**
* Maps scalar columns to enums
*
* @ignore
* @psalm-var array<string, string>
*/
public $enumMappings = [];

/**
* Maps column names in the result set to the alias/field type to use in the mapped result.
*
Expand Down Expand Up @@ -384,6 +392,21 @@ public function addScalarResult($columnName, $alias, $type = 'string')
return $this;
}

/**
* Adds a scalar result mapping.
*
* @param string $columnName The name of the column in the SQL result set.
* @param string $enumType The enum type
*
* @return $this
*/
public function addEnumResult($columnName, $enumType)
{
$this->enumMappings[$columnName] = $enumType;

return $this;
}

/**
* Adds a metadata parameter mappings.
*
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,10 @@ public function walkSelectExpression($selectExpression)
if (! $hidden) {
$this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldMapping['type']);
$this->scalarFields[$dqlAlias][$fieldName] = $columnAlias;

if (! empty($fieldMapping['enumType'])) {
$this->rsm->addEnumResult($columnAlias, $fieldMapping['enumType']);
}
}

break;
Expand Down
49 changes: 49 additions & 0 deletions tests/Doctrine/Tests/Models/Enums/CardWithNullable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\Enums;

use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;

/** @Entity */
#[Entity]
class CardWithNullable
{
/**
* @Id @GeneratedValue @Column(type="integer")
* @var int
*/
#[Id, GeneratedValue, Column(type: 'integer')]
public $id;

/**
* @Column(type="string", enumType=Suit::class, nullable=true)
* @var ?Suit
*/
#[Column(type: 'string', nullable: true, enumType: Suit::class)]
public $suit;

public static function loadMetadata(ClassMetadataInfo $metadata): void
{
$metadata->mapField(
[
'id' => true,
'fieldName' => 'id',
'type' => 'integer',
]
);
$metadata->mapField(
[
'fieldName' => 'suit',
'type' => 'string',
'enumType' => Suit::class,
'nullable' => true,
]
);
}
}
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Models/Enums/Scale.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Scale

/**
* @Column(type="simple_array", enumType=Unit::class)
* @var Unit
* @var Unit[]
*/
#[Column(type: 'simple_array', enumType: Unit::class)]
public $supportedUnits;
Expand Down
54 changes: 54 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\Models\Enums\Card;
use Doctrine\Tests\Models\Enums\CardWithDefault;
use Doctrine\Tests\Models\Enums\CardWithNullable;
use Doctrine\Tests\Models\Enums\Product;
use Doctrine\Tests\Models\Enums\Quantity;
use Doctrine\Tests\Models\Enums\Scale;
Expand Down Expand Up @@ -61,6 +62,59 @@ public function testEnumMapping(string $cardClass): void
$this->assertEquals(Suit::Clubs, $fetchedCard->suit);
}

public function testEnumHydration(): void
{
$this->setUpEntitySchema([Card::class, CardWithNullable::class]);

$card = new Card();
$card->suit = Suit::Clubs;

$cardWithNullable = new CardWithNullable();
$cardWithNullable->suit = null;

$this->_em->persist($card);
$this->_em->persist($cardWithNullable);
$this->_em->flush();
$this->_em->clear();

$result = $this->_em->createQueryBuilder()
->from(Card::class, 'c')
->select('c.id, c.suit')
->getQuery()
->getResult();

$this->assertInstanceOf(Suit::class, $result[0]['suit']);
$this->assertEquals(Suit::Clubs, $result[0]['suit']);

$result = $this->_em->createQueryBuilder()
->from(CardWithNullable::class, 'c')
->select('c.id, c.suit')
->getQuery()
->getResult();

$this->assertNull($result[0]['suit']);
}

public function testEnumArrayHydration(): void
{
$this->setUpEntitySchema([Scale::class]);

$scale = new Scale();
$scale->supportedUnits = [Unit::Gram, Unit::Meter];

$this->_em->persist($scale);
$this->_em->flush();
$this->_em->clear();

$result = $this->_em->createQueryBuilder()
->from(Scale::class, 's')
->select('s.id, s.supportedUnits')
->getQuery()
->getResult();

self::assertEqualsCanonicalizing([Unit::Gram, Unit::Meter], $result[0]['supportedUnits']);
}

public function testFindByEnum(): void
{
$this->setUpEntitySchema([Card::class]);
Expand Down