Skip to content

Commit

Permalink
Ensure consistent original data with enums
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Dec 7, 2022
1 parent a2b5bae commit fb85326
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ protected function registerManaged(ClassMetadata $class, $entity, array $data)
*
* @return BackedEnum|array<BackedEnum>
*/
private function buildEnum($value, string $enumType)
final protected function buildEnum($value, string $enumType)
{
if (is_array($value)) {
return array_map(static function ($value) use ($enumType): BackedEnum {
Expand Down
17 changes: 17 additions & 0 deletions lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use Doctrine\ORM\Internal\SQLResultCasing;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\Query;
use Exception;
use RuntimeException;
use ValueError;

use function array_keys;
use function array_search;
Expand Down Expand Up @@ -140,6 +142,21 @@ protected function hydrateRowData(array $row, array &$result)
$value = $type->convertToPHPValue($value, $this->_platform);
}

if ($value !== null && isset($cacheKeyInfo['enumType'])) {
$originalValue = $value;
try {
$value = $this->buildEnum($originalValue, $cacheKeyInfo['enumType']);
} catch (ValueError $e) {
throw MappingException::invalidEnumValue(
$entityName,
$cacheKeyInfo['fieldName'],
(string) $originalValue,
$cacheKeyInfo['enumType'],
$e
);
}
}

$fieldName = $cacheKeyInfo['fieldName'];

// Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator)
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,9 @@ protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r'
$columnAlias = $this->getSQLColumnAlias($fieldMapping['columnName']);

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

if (isset($fieldMapping['requireSQLConversion'])) {
$type = Type::getType($fieldMapping['type']);
Expand Down

0 comments on commit fb85326

Please sign in to comment.