Skip to content

Commit 9c57bed

Browse files
author
Pavel.Batanov
committed
Allow null to be stored as enum value
1 parent 6d8c361 commit 9c57bed

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/DBAL/EnumType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ private function getStrategy(): NamingStrategyInterface
4848
*/
4949
public function convertToPHPValue($value, AbstractPlatform $platform)
5050
{
51+
if (null === $value) {
52+
return null;
53+
}
54+
5155
/** @var Enum $fqcn */
5256
$fqcn = $this->fqcn;
5357

@@ -63,6 +67,10 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
6367
*/
6468
public function convertToDatabaseValue($value, AbstractPlatform $platform)
6569
{
70+
if (null === $value) {
71+
return null;
72+
}
73+
6674
if (!is_a($value, $this->fqcn)) {
6775
throw ConversionException::conversionFailed($value, $this->getName());
6876
}

tests/Unit/DBAL/EnumTypeTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class EnumTypeTest extends TestCase
2525
* @param string $value
2626
* @param TestEnum $expectedStatus
2727
*/
28-
public function testConvertToPHPValue(string $value, TestEnum $expectedStatus): void
28+
public function testConvertToPHPValue(?string $value, ?TestEnum $expectedStatus): void
2929
{
3030
$actualValue = self::createType()->convertToPHPValue($value, $this->createPlatformMock());
3131

@@ -38,7 +38,7 @@ public function testConvertToPHPValue(string $value, TestEnum $expectedStatus):
3838
* @param string $expectedStatus
3939
* @param TestEnum $status
4040
*/
41-
public function testConvertToDatabaseValue(string $expectedStatus, TestEnum $status): void
41+
public function testConvertToDatabaseValue(?string $expectedStatus, ?TestEnum $status): void
4242
{
4343
$actualValue = self::createType()->convertToDatabaseValue($status, $this->createPlatformMock());
4444

@@ -50,6 +50,7 @@ public function getObjectToValueMap(): array
5050
return [
5151
['ONE', TestEnum::ONE()],
5252
['TWO', TestEnum::TWO()],
53+
[null, null],
5354
];
5455
}
5556

0 commit comments

Comments
 (0)