Fix exception thrown by Form when converting UUID

This commit is contained in:
Jérémy Derussé 2020-12-09 16:28:16 +01:00
parent e974752280
commit a41e7f0771
No known key found for this signature in database
GPG Key ID: 2083FA5758C473D2
2 changed files with 43 additions and 1 deletions

View File

@ -12,8 +12,10 @@
namespace Symfony\Bridge\Doctrine\Form\ChoiceList;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Exception\TransformationFailedException;
/**
* Loads entities using a {@link QueryBuilder} instance.
@ -96,7 +98,11 @@ class ORMQueryBuilderLoader implements EntityLoaderInterface
$doctrineType = Type::getType($type);
$platform = $qb->getEntityManager()->getConnection()->getDatabasePlatform();
foreach ($values as &$value) {
$value = $doctrineType->convertToDatabaseValue($value, $platform);
try {
$value = $doctrineType->convertToDatabaseValue($value, $platform);
} catch (ConversionException $e) {
throw new TransformationFailedException(sprintf('Failed to transform "%s" into "%s".', $value, $type), 0, $e);
}
}
unset($value);
}

View File

@ -20,6 +20,7 @@ use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
use Symfony\Bridge\Doctrine\Types\UlidType;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Uid\Uuid;
class ORMQueryBuilderLoaderTest extends TestCase
@ -188,6 +189,41 @@ class ORMQueryBuilderLoaderTest extends TestCase
$loader->getEntitiesByIds('id', ['71c5fd46-3f16-4abb-bad7-90ac1e654a2d', '', 'b98e8e11-2897-44df-ad24-d2627eb7f499']);
}
/**
* @dataProvider provideUidEntityClasses
*/
public function testUidThrowProperException($entityClass)
{
if (Type::hasType('uuid')) {
Type::overrideType('uuid', UuidType::class);
} else {
Type::addType('uuid', UuidType::class);
}
if (!Type::hasType('ulid')) {
Type::addType('ulid', UlidType::class);
}
$em = DoctrineTestHelper::createTestEntityManager();
$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
->setConstructorArgs([$em])
->setMethods(['getQuery'])
->getMock();
$qb->expects($this->never())
->method('getQuery');
$qb->select('e')
->from($entityClass, 'e');
$loader = new ORMQueryBuilderLoader($qb);
$this->expectException(TransformationFailedException::class);
$this->expectExceptionMessageMatches('/^Failed to transform "hello" into "(uuid|ulid)"\.$/');
$loader->getEntitiesByIds('id', ['hello']);
}
public function testEmbeddedIdentifierName()
{
if (Version::compare('2.5.0') > 0) {