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

[DoctrineBridge] Fix form EntityType with filter on UID #39210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\Doctrine\Form\ChoiceList;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\QueryBuilder;

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

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

// Like above, but we just filter out empty strings.
$values = array_values(array_filter($values, function ($v) {
return '' !== (string) $v;
}));

// Convert values into right type
if (Type::hasType($type)) {
$doctrineType = Type::getType($type);
$platform = $qb->getEntityManager()->getConnection()->getDatabasePlatform();
foreach ($values as &$value) {
$value = $doctrineType->convertToDatabaseValue($value, $platform);
}
unset($value);
}
} else {
$parameterType = Connection::PARAM_STR_ARRAY;
}
Expand Down
28 changes: 28 additions & 0 deletions 28 src/Symfony/Bridge/Doctrine/Tests/Fixtures/UlidIdEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Fixtures;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;

/** @Entity */
class UlidIdEntity
{
/** @Id @Column(type="ulid") */
protected $id;

public function __construct($id)
{
$this->id = $id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@
namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\GuidType;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Version;
use PHPUnit\Framework\TestCase;
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\Uid\Uuid;

class ORMQueryBuilderLoaderTest extends TestCase
{
protected function tearDown(): void
{
if (Type::hasType('uuid')) {
Type::overrideType('uuid', GuidType::class);
}
}

public function testIdentifierTypeIsStringArray()
{
$this->checkIdentifierType('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity', Connection::PARAM_STR_ARRAY);
Expand Down Expand Up @@ -131,6 +143,51 @@ public function testFilterEmptyUuids($entityClass)
$loader->getEntitiesByIds('id', ['71c5fd46-3f16-4abb-bad7-90ac1e654a2d', '', 'b98e8e11-2897-44df-ad24-d2627eb7f499']);
}

/**
* @dataProvider provideUidEntityClasses
*/
public function testFilterUid($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();

$query = $this->getMockBuilder('QueryMock')
->setMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
->getMock();

$query
->method('getResult')
->willReturn([]);

$query->expects($this->once())
->method('setParameter')
->with('ORMQueryBuilderLoader_getEntitiesByIds_id', [Uuid::fromString('71c5fd46-3f16-4abb-bad7-90ac1e654a2d')->toBinary(), Uuid::fromString('b98e8e11-2897-44df-ad24-d2627eb7f499')->toBinary()], Connection::PARAM_STR_ARRAY)
->willReturn($query);

$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
->setConstructorArgs([$em])
->setMethods(['getQuery'])
->getMock();

$qb->expects($this->once())
->method('getQuery')
->willReturn($query);

$qb->select('e')
->from($entityClass, 'e');

$loader = new ORMQueryBuilderLoader($qb);
$loader->getEntitiesByIds('id', ['71c5fd46-3f16-4abb-bad7-90ac1e654a2d', '', 'b98e8e11-2897-44df-ad24-d2627eb7f499']);
}

public function testEmbeddedIdentifierName()
{
if (Version::compare('2.5.0') > 0) {
Expand Down Expand Up @@ -176,4 +233,12 @@ public function provideGuidEntityClasses()
['Symfony\Bridge\Doctrine\Tests\Fixtures\UuidIdEntity'],
];
}

public function provideUidEntityClasses()
{
return [
['Symfony\Bridge\Doctrine\Tests\Fixtures\UuidIdEntity'],
['Symfony\Bridge\Doctrine\Tests\Fixtures\UlidIdEntity'],
];
}
}
6 changes: 5 additions & 1 deletion 6 src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ final class UlidTypeTest extends TestCase

public static function setUpBeforeClass(): void
{
Type::addType('ulid', UlidType::class);
if (Type::hasType('ulid')) {
Type::overrideType('ulid', UlidType::class);
} else {
Type::addType('ulid', UlidType::class);
}
}

protected function setUp(): void
Expand Down
6 changes: 5 additions & 1 deletion 6 src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ final class UuidTypeTest extends TestCase

public static function setUpBeforeClass(): void
{
Type::addType('uuid', UuidType::class);
if (Type::hasType('uuid')) {
Type::overrideType('uuid', UuidType::class);
} else {
Type::addType('uuid', UuidType::class);
}
}

protected function setUp(): void
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.