Skip to content

Commit

Permalink
fix enum instance after entity deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich authored and VasekPurchart committed Apr 17, 2018
1 parent 7e2d9ed commit 7bbce8f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Enum/EnumPostLoadEntityListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ private function processField(
)
{
$metadata = $this->getClassMetadata($entityManager, get_class($entity));
$scalarValue = $metadata->getFieldValue($entity, $fieldName);
if (!is_scalar($scalarValue)) {
return;
$enumValue = $metadata->getFieldValue($entity, $fieldName);

if ($enumValue === null) {
return null;
}

$enum = $enumClassName::get($scalarValue);
$enum = $enumClassName::get(($enumValue instanceof Enum) ? $enumValue->getValue() : $enumValue);
$metadata->setFieldValue($entity, $fieldName, $enum);
$entityManager->getUnitOfWork()->setOriginalEntityProperty(
spl_object_hash($entity),
Expand Down
12 changes: 12 additions & 0 deletions tests/Enum/LoadEnumToEntityIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ public function testLoadNullEnumToEntity()
$this->assertNull($foo->getNullableEnum());
}

public function testLoadEnumToUnserializedEntity()
{
$fooBeforeSerialization = new FooEntity();
$fooBeforeSerialization->setEnum(FooEnum::get(FooEnum::ONE));

$foo = unserialize(serialize($fooBeforeSerialization));
$this->callPostLoadEventOnEntity($foo);

$this->assertSame(FooEnum::get(FooEnum::ONE), $foo->getEnum());
$this->assertNull($foo->getNullableEnum());
}

public function testMultipleLoadEvents()
{
$foo = new FooEntity();
Expand Down
5 changes: 5 additions & 0 deletions tests/Enum/data/FooEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function __construct()
$this->embedded = new FooEmbeddable();
}

public function setEnum(FooEnum $enum)
{
$this->enum = $enum;
}

public function getEnum(): FooEnum
{
return $this->enum;
Expand Down

0 comments on commit 7bbce8f

Please sign in to comment.