Skip to content

Commit b3ea174

Browse files
committed
Fix form EntotyType with uid
1 parent 79125d8 commit b3ea174

File tree

5 files changed

+116
-4
lines changed

5 files changed

+116
-4
lines changed

Form/ChoiceList/ORMQueryBuilderLoader.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Doctrine\Form\ChoiceList;
1313

1414
use Doctrine\DBAL\Connection;
15+
use Doctrine\DBAL\Types\Type;
1516
use Doctrine\ORM\QueryBuilder;
1617

1718
/**
@@ -74,21 +75,31 @@ public function getEntitiesByIds(string $identifier, array $values)
7475
// Guess type
7576
$entity = current($qb->getRootEntities());
7677
$metadata = $qb->getEntityManager()->getClassMetadata($entity);
77-
if (\in_array($metadata->getTypeOfField($identifier), ['integer', 'bigint', 'smallint'])) {
78+
if (\in_array($type = $metadata->getTypeOfField($identifier), ['integer', 'bigint', 'smallint'])) {
7879
$parameterType = Connection::PARAM_INT_ARRAY;
7980

8081
// Filter out non-integer values (e.g. ""). If we don't, some
8182
// databases such as PostgreSQL fail.
8283
$values = array_values(array_filter($values, function ($v) {
8384
return (string) $v === (string) (int) $v || ctype_digit($v);
8485
}));
85-
} elseif (\in_array($metadata->getTypeOfField($identifier), ['uuid', 'guid'])) {
86+
} elseif (\in_array($type, ['ulid', 'uuid', 'guid'])) {
8687
$parameterType = Connection::PARAM_STR_ARRAY;
8788

8889
// Like above, but we just filter out empty strings.
8990
$values = array_values(array_filter($values, function ($v) {
9091
return '' !== (string) $v;
9192
}));
93+
94+
// Convert values into right type
95+
if (Type::hasType($type)) {
96+
$doctrineType = Type::getType($type);
97+
$platform = $qb->getEntityManager()->getConnection()->getDatabasePlatform();
98+
foreach ($values as &$value) {
99+
$value = $doctrineType->convertToDatabaseValue($value, $platform);
100+
}
101+
unset($value);
102+
}
92103
} else {
93104
$parameterType = Connection::PARAM_STR_ARRAY;
94105
}

Tests/Fixtures/UlidIdEntity.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Doctrine\ORM\Mapping\Column;
15+
use Doctrine\ORM\Mapping\Entity;
16+
use Doctrine\ORM\Mapping\Id;
17+
18+
/** @Entity */
19+
class UlidIdEntity
20+
{
21+
/** @Id @Column(type="ulid") */
22+
protected $id;
23+
24+
public function __construct($id)
25+
{
26+
$this->id = $id;
27+
}
28+
}

Tests/Form/ChoiceList/ORMQueryBuilderLoaderTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,25 @@
1212
namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList;
1313

1414
use Doctrine\DBAL\Connection;
15+
use Doctrine\DBAL\Types\GuidType;
16+
use Doctrine\DBAL\Types\Type;
1517
use Doctrine\ORM\Version;
1618
use PHPUnit\Framework\TestCase;
1719
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
1820
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
21+
use Symfony\Bridge\Doctrine\Types\UlidType;
22+
use Symfony\Bridge\Doctrine\Types\UuidType;
23+
use Symfony\Component\Uid\Uuid;
1924

2025
class ORMQueryBuilderLoaderTest extends TestCase
2126
{
27+
protected function tearDown(): void
28+
{
29+
if (Type::hasType('uuid')) {
30+
Type::overrideType('uuid', GuidType::class);
31+
}
32+
}
33+
2234
public function testIdentifierTypeIsStringArray()
2335
{
2436
$this->checkIdentifierType('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity', Connection::PARAM_STR_ARRAY);
@@ -131,6 +143,51 @@ public function testFilterEmptyUuids($entityClass)
131143
$loader->getEntitiesByIds('id', ['71c5fd46-3f16-4abb-bad7-90ac1e654a2d', '', 'b98e8e11-2897-44df-ad24-d2627eb7f499']);
132144
}
133145

146+
/**
147+
* @dataProvider provideUidEntityClasses
148+
*/
149+
public function testFilterUid($entityClass)
150+
{
151+
if (Type::hasType('uuid')) {
152+
Type::overrideType('uuid', UuidType::class);
153+
} else {
154+
Type::addType('uuid', UuidType::class);
155+
}
156+
if (!Type::hasType('ulid')) {
157+
Type::addType('ulid', UlidType::class);
158+
}
159+
160+
$em = DoctrineTestHelper::createTestEntityManager();
161+
162+
$query = $this->getMockBuilder('QueryMock')
163+
->setMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
164+
->getMock();
165+
166+
$query
167+
->method('getResult')
168+
->willReturn([]);
169+
170+
$query->expects($this->once())
171+
->method('setParameter')
172+
->with('ORMQueryBuilderLoader_getEntitiesByIds_id', [Uuid::fromString('71c5fd46-3f16-4abb-bad7-90ac1e654a2d')->toBinary(), Uuid::fromString('b98e8e11-2897-44df-ad24-d2627eb7f499')->toBinary()], Connection::PARAM_STR_ARRAY)
173+
->willReturn($query);
174+
175+
$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
176+
->setConstructorArgs([$em])
177+
->setMethods(['getQuery'])
178+
->getMock();
179+
180+
$qb->expects($this->once())
181+
->method('getQuery')
182+
->willReturn($query);
183+
184+
$qb->select('e')
185+
->from($entityClass, 'e');
186+
187+
$loader = new ORMQueryBuilderLoader($qb);
188+
$loader->getEntitiesByIds('id', ['71c5fd46-3f16-4abb-bad7-90ac1e654a2d', '', 'b98e8e11-2897-44df-ad24-d2627eb7f499']);
189+
}
190+
134191
public function testEmbeddedIdentifierName()
135192
{
136193
if (Version::compare('2.5.0') > 0) {
@@ -176,4 +233,12 @@ public function provideGuidEntityClasses()
176233
['Symfony\Bridge\Doctrine\Tests\Fixtures\UuidIdEntity'],
177234
];
178235
}
236+
237+
public function provideUidEntityClasses()
238+
{
239+
return [
240+
['Symfony\Bridge\Doctrine\Tests\Fixtures\UuidIdEntity'],
241+
['Symfony\Bridge\Doctrine\Tests\Fixtures\UlidIdEntity'],
242+
];
243+
}
179244
}

Tests/Types/UlidTypeTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ final class UlidTypeTest extends TestCase
3131

3232
public static function setUpBeforeClass(): void
3333
{
34-
Type::addType('ulid', UlidType::class);
34+
if (Type::hasType('ulid')) {
35+
Type::overrideType('ulid', UlidType::class);
36+
} else {
37+
Type::addType('ulid', UlidType::class);
38+
}
3539
}
3640

3741
protected function setUp(): void

Tests/Types/UuidTypeTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ final class UuidTypeTest extends TestCase
3131

3232
public static function setUpBeforeClass(): void
3333
{
34-
Type::addType('uuid', UuidType::class);
34+
if (Type::hasType('uuid')) {
35+
Type::overrideType('uuid', UuidType::class);
36+
} else {
37+
Type::addType('uuid', UuidType::class);
38+
}
3539
}
3640

3741
protected function setUp(): void

0 commit comments

Comments
 (0)