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 762ecda

Browse filesBrowse files
committed
Fix form EntotyType with uid
1 parent f0aa4e7 commit 762ecda
Copy full SHA for 762ecda

File tree

3 files changed

+104
-1
lines changed
Filter options

3 files changed

+104
-1
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
+64Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,24 @@
1212
namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList;
1313

1414
use Doctrine\DBAL\Connection;
15+
use Doctrine\DBAL\Types\Type;
1516
use Doctrine\ORM\Version;
1617
use PHPUnit\Framework\TestCase;
1718
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
1819
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
20+
use Symfony\Bridge\Doctrine\Types\UlidType;
21+
use Symfony\Bridge\Doctrine\Types\UuidType;
22+
use Symfony\Component\Uid\Uuid;
1923

2024
class ORMQueryBuilderLoaderTest extends TestCase
2125
{
26+
protected function tearDown(): void
27+
{
28+
if (Type::hasType('uuid')) {
29+
Type::overrideType('uuid', GuidType::class);
30+
}
31+
}
32+
2233
public function testIdentifierTypeIsStringArray()
2334
{
2435
$this->checkIdentifierType('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity', Connection::PARAM_STR_ARRAY);
@@ -131,6 +142,51 @@ public function testFilterEmptyUuids($entityClass)
131142
$loader->getEntitiesByIds('id', ['71c5fd46-3f16-4abb-bad7-90ac1e654a2d', '', 'b98e8e11-2897-44df-ad24-d2627eb7f499']);
132143
}
133144

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.