Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6145108

Browse files
committed
Fix custom type based on Uid on entity loader
1 parent 315b0db commit 6145108

5 files changed

Lines changed: 82 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\DBAL\Types\ConversionException;
1717
use Doctrine\DBAL\Types\Type;
1818
use Doctrine\ORM\QueryBuilder;
19+
use Symfony\Bridge\Doctrine\Types\AbstractUidType;
1920
use Symfony\Component\Form\Exception\TransformationFailedException;
2021

2122
/**
@@ -68,7 +69,7 @@ public function getEntitiesByIds(string $identifier, array $values): array
6869
// Filter out non-integer values (e.g. ""). If we don't, some
6970
// databases such as PostgreSQL fail.
7071
$values = array_values(array_filter($values, static fn ($v) => \is_string($v) && ctype_digit($v) || (string) $v === (string) (int) $v));
71-
} elseif (\in_array($type, ['ulid', 'uuid', 'guid'])) {
72+
} elseif (\in_array($type, ['ulid', 'uuid', 'guid']) || (Type::hasType($type) && is_subclass_of(Type::getType($type), AbstractUidType::class))) {
7273
$parameterType = class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY;
7374

7475
// Like above, but we just filter out empty strings.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 Symfony\Component\Uid\Uuid;
15+
16+
final class CustomUuidId extends Uuid
17+
{
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 CustomUuidIdEntity
20+
{
21+
public function __construct(
22+
#[Id, Column(type: CustomUuidIdType::class)]
23+
protected CustomUuidId $id,
24+
) {
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 Symfony\Bridge\Doctrine\Types\AbstractUidType;
15+
16+
final class CustomUuidIdType extends AbstractUidType
17+
{
18+
public function getName(): string
19+
{
20+
return self::class;
21+
}
22+
23+
protected function getUidClass(): string
24+
{
25+
return CustomUuidId::class;
26+
}
27+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PHPUnit\Framework\TestCase;
2323
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
2424
use Symfony\Bridge\Doctrine\Tests\DoctrineTestHelper;
25+
use Symfony\Bridge\Doctrine\Tests\Fixtures\CustomUuidIdType;
2526
use Symfony\Bridge\Doctrine\Tests\Fixtures\EmbeddedIdentifierEntity;
2627
use Symfony\Bridge\Doctrine\Tests\Fixtures\LegacyQueryMock;
2728
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
@@ -159,6 +160,9 @@ public function testFilterUid(string $entityClass)
159160
if (!Type::hasType('ulid')) {
160161
Type::addType('ulid', UlidType::class);
161162
}
163+
if (!Type::hasType(CustomUuidIdType::class)) {
164+
Type::addType(CustomUuidIdType::class, CustomUuidIdType::class);
165+
}
162166

163167
$em = DoctrineTestHelper::createTestEntityManager();
164168

@@ -202,6 +206,9 @@ public function testUidThrowProperException(string $entityClass)
202206
if (!Type::hasType('ulid')) {
203207
Type::addType('ulid', UlidType::class);
204208
}
209+
if (!Type::hasType(CustomUuidIdType::class)) {
210+
Type::addType(CustomUuidIdType::class, CustomUuidIdType::class);
211+
}
205212

206213
$em = DoctrineTestHelper::createTestEntityManager();
207214

@@ -219,7 +226,7 @@ public function testUidThrowProperException(string $entityClass)
219226
$loader = new ORMQueryBuilderLoader($qb);
220227

221228
$this->expectException(TransformationFailedException::class);
222-
$this->expectExceptionMessageMatches('/^Failed to transform "hello" into "(uuid|ulid)"\.$/');
229+
$this->expectExceptionMessageMatches('/^Failed to transform "hello" into "(uuid|ulid|Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\CustomUuidIdType)"\.$/');
223230

224231
$loader->getEntitiesByIds('id', ['hello']);
225232
}
@@ -267,6 +274,7 @@ public static function provideUidEntityClasses(): array
267274
return [
268275
['Symfony\Bridge\Doctrine\Tests\Fixtures\UuidIdEntity'],
269276
['Symfony\Bridge\Doctrine\Tests\Fixtures\UlidIdEntity'],
277+
['Symfony\Bridge\Doctrine\Tests\Fixtures\CustomUuidIdEntity'],
270278
];
271279
}
272280

0 commit comments

Comments
 (0)