Skip to content

Commit

Permalink
Make ReflectionEnumProperty::getValue return the enum
Browse files Browse the repository at this point in the history
UoW compares orig and actual value. Orig value is the enum. But without this change, actual value is the inner value of the BackedEnum. Therefore, Entities with enum field are always considered a change.
  • Loading branch information
simPod committed Oct 13, 2022
1 parent 13d1c7b commit 1f99f81
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/Doctrine/ORM/Id/AssignedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ORM\Id;

use BackedEnum;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\EntityMissingAssignedId;

Expand Down Expand Up @@ -39,6 +40,10 @@ public function generateId(EntityManagerInterface $em, $entity)
$value = $em->getUnitOfWork()->getSingleIdentifierValue($value);
}

if ($value instanceof BackedEnum) {
$value = $value->value;
}

$identifier[$idField] = $value;
}

Expand Down
10 changes: 2 additions & 8 deletions lib/Doctrine/ORM/Mapping/ReflectionEnumProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(ReflectionProperty $originalReflectionProperty, stri
*
* @param object|null $object
*
* @return int|string|int[]|string[]|null
* @return BackedEnum|null
*/
#[ReturnTypeWillChange]
public function getValue($object = null)
Expand All @@ -53,13 +53,7 @@ public function getValue($object = null)
return null;
}

if (is_array($enum)) {
return array_map(static function (BackedEnum $item): mixed {
return $item->value;
}, $enum);
}

return $enum->value;
return $enum;
}

/**
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 @@ -647,6 +647,9 @@ protected function prepareUpdateData($entity, bool $isInsert = false)
}

$newVal = $change[1];
if ($newVal instanceof BackedEnum) {
$newVal = $newVal->value;
}

if (! isset($this->class->associationMappings[$field])) {
$fieldMapping = $this->class->fieldMappings[$field];
Expand Down

0 comments on commit 1f99f81

Please sign in to comment.