Skip to content

Commit a41e7f0

Browse files
committed
Fix exception thrown by Form when converting UUID
1 parent e974752 commit a41e7f0

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Symfony/Bridge/Doctrine/Form/ChoiceList/ORMQueryBuilderLoader.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
namespace Symfony\Bridge\Doctrine\Form\ChoiceList;
1313

1414
use Doctrine\DBAL\Connection;
15+
use Doctrine\DBAL\Types\ConversionException;
1516
use Doctrine\DBAL\Types\Type;
1617
use Doctrine\ORM\QueryBuilder;
18+
use Symfony\Component\Form\Exception\TransformationFailedException;
1719

1820
/**
1921
* Loads entities using a {@link QueryBuilder} instance.
@@ -96,7 +98,11 @@ public function getEntitiesByIds(string $identifier, array $values)
9698
$doctrineType = Type::getType($type);
9799
$platform = $qb->getEntityManager()->getConnection()->getDatabasePlatform();
98100
foreach ($values as &$value) {
99-
$value = $doctrineType->convertToDatabaseValue($value, $platform);
101+
try {
102+
$value = $doctrineType->convertToDatabaseValue($value, $platform);
103+
} catch (ConversionException $e) {
104+
throw new TransformationFailedException(sprintf('Failed to transform "%s" into "%s".', $value, $type), 0, $e);
105+
}
100106
}
101107
unset($value);
102108
}

src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/ORMQueryBuilderLoaderTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
2121
use Symfony\Bridge\Doctrine\Types\UlidType;
2222
use Symfony\Bridge\Doctrine\Types\UuidType;
23+
use Symfony\Component\Form\Exception\TransformationFailedException;
2324
use Symfony\Component\Uid\Uuid;
2425

2526
class ORMQueryBuilderLoaderTest extends TestCase
@@ -188,6 +189,41 @@ public function testFilterUid($entityClass)
188189
$loader->getEntitiesByIds('id', ['71c5fd46-3f16-4abb-bad7-90ac1e654a2d', '', 'b98e8e11-2897-44df-ad24-d2627eb7f499']);
189190
}
190191

192+
/**
193+
* @dataProvider provideUidEntityClasses
194+
*/
195+
public function testUidThrowProperException($entityClass)
196+
{
197+
if (Type::hasType('uuid')) {
198+
Type::overrideType('uuid', UuidType::class);
199+
} else {
200+
Type::addType('uuid', UuidType::class);
201+
}
202+
if (!Type::hasType('ulid')) {
203+
Type::addType('ulid', UlidType::class);
204+
}
205+
206+
$em = DoctrineTestHelper::createTestEntityManager();
207+
208+
$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
209+
->setConstructorArgs([$em])
210+
->setMethods(['getQuery'])
211+
->getMock();
212+
213+
$qb->expects($this->never())
214+
->method('getQuery');
215+
216+
$qb->select('e')
217+
->from($entityClass, 'e');
218+
219+
$loader = new ORMQueryBuilderLoader($qb);
220+
221+
$this->expectException(TransformationFailedException::class);
222+
$this->expectExceptionMessageMatches('/^Failed to transform "hello" into "(uuid|ulid)"\.$/');
223+
224+
$loader->getEntitiesByIds('id', ['hello']);
225+
}
226+
191227
public function testEmbeddedIdentifierName()
192228
{
193229
if (Version::compare('2.5.0') > 0) {

0 commit comments

Comments
 (0)