Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b95814a

Browse filesBrowse files
committed
Fix form EntotyType with uid
1 parent fd05da6 commit b95814a
Copy full SHA for b95814a

File tree

5 files changed

+115
-3
lines changed
Filter options

5 files changed

+115
-3
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Form/ChoiceList/ORMQueryBuilderLoader.php
+12-1Lines changed: 12 additions & 1 deletion
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
/**
@@ -82,13 +83,23 @@ public function getEntitiesByIds(string $identifier, array $values)
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 = $metadata->getTypeOfField($identifier), ['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+
$plateform = $qb->getEntityManager()->getConnection()->getDatabasePlatform();
98+
foreach ($values as &$value) {
99+
$value = $doctrineType->convertToDatabaseValue($value, $plateform);
100+
}
101+
unset($value);
102+
}
92103
} else {
93104
$parameterType = Connection::PARAM_STR_ARRAY;
94105
}
+28Lines 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 <fabien@symfony.com>
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+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/ORMQueryBuilderLoaderTest.php
+65Lines 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
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php
+5-1Lines 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

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php
+5-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.