Skip to content

Commit 5ca43b8

Browse files
bug #38600 [DoctrineBridge] Convert values to Rfc4122 before inserting them into the database (Kai)
This PR was squashed before being merged into the 5.x branch. Discussion ---------- [DoctrineBridge] Convert values to Rfc4122 before inserting them into the database | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #38599 | License | MIT This PR formats the ULID into RFC4211 before inserting it into the database to avoid insertion failure on Postgres due to not recognized formating. Commits ------- fd3a6e8 [DoctrineBridge] Convert values to Rfc4122 before inserting them into the database
2 parents abbb3d0 + fd3a6e8 commit 5ca43b8

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testUlidConvertsToDatabaseValue(): void
4848
{
4949
$ulid = Ulid::fromString(self::DUMMY_ULID);
5050

51-
$expected = $ulid->__toString();
51+
$expected = $ulid->toRfc4122();
5252
$actual = $this->type->convertToDatabaseValue($ulid, $this->platform);
5353

5454
$this->assertEquals($expected, $actual);
@@ -60,7 +60,7 @@ public function testUlidInterfaceConvertsToDatabaseValue(): void
6060

6161
$ulid
6262
->expects($this->once())
63-
->method('__toString')
63+
->method('toRfc4122')
6464
->willReturn('foo');
6565

6666
$actual = $this->type->convertToDatabaseValue($ulid, $this->platform);
@@ -71,8 +71,11 @@ public function testUlidInterfaceConvertsToDatabaseValue(): void
7171
public function testUlidStringConvertsToDatabaseValue(): void
7272
{
7373
$actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
74+
$ulid = Ulid::fromString(self::DUMMY_ULID);
75+
76+
$expected = $ulid->toRfc4122();
7477

75-
$this->assertEquals(self::DUMMY_ULID, $actual);
78+
$this->assertEquals($expected, $actual);
7679
}
7780

7881
public function testInvalidUlidConversionForDatabaseValue(): void

src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testUuidInterfaceConvertsToDatabaseValue(): void
6060

6161
$uuid
6262
->expects($this->once())
63-
->method('__toString')
63+
->method('toRfc4122')
6464
->willReturn('foo');
6565

6666
$actual = $this->type->convertToDatabaseValue($uuid, $this->platform);

src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,21 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
5656
}
5757

5858
if ($value instanceof AbstractUid) {
59-
return (string) $value;
59+
return $value->toRfc4122();
6060
}
6161

6262
if (!\is_string($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
6363
return null;
6464
}
6565

6666
if ($this->getUidClass()::isValid((string) $value)) {
67-
return (string) $value;
67+
try {
68+
$uuid = $this->getUidClass()::fromString($value);
69+
70+
return $uuid->toRfc4122();
71+
} catch (\InvalidArgumentException $e) {
72+
throw ConversionException::conversionFailed($value, $this->getName());
73+
}
6874
}
6975

7076
throw ConversionException::conversionFailed($value, $this->getName());

0 commit comments

Comments
 (0)