Skip to content

Commit 27e8cd2

Browse files
committed
Filtering empty uuids in ORMQueryBuilderLoader.
1 parent c951ca6 commit 27e8cd2

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function getEntitiesByIds($identifier, array $values)
106106
$values = array_values(array_filter($values, function ($v) {
107107
return (string) $v === (string) (int) $v || ctype_digit($v);
108108
}));
109-
} elseif ('guid' === $metadata->getTypeOfField($identifier)) {
109+
} elseif (in_array($metadata->getTypeOfField($identifier), array('uuid', 'guid'))) {
110110
$parameterType = Connection::PARAM_STR_ARRAY;
111111

112112
// Like above, but we just filter out empty strings.
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\Id;
15+
use Doctrine\ORM\Mapping\Column;
16+
use Doctrine\ORM\Mapping\Entity;
17+
18+
/** @Entity */
19+
class GuidIdEntity
20+
{
21+
/** @Id @Column(type="guid") */
22+
protected $id;
23+
24+
public function __construct($id)
25+
{
26+
$this->id = $id;
27+
}
28+
}
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\Id;
15+
use Doctrine\ORM\Mapping\Column;
16+
use Doctrine\ORM\Mapping\Entity;
17+
18+
/** @Entity */
19+
class UuidIdEntity
20+
{
21+
/** @Id @Column(type="uuid") */
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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,38 @@ public function testFilterNonIntegerValues()
107107
$loader->getEntitiesByIds('id', array(1, '', 2, 3, 'foo', '9223372036854775808'));
108108
}
109109

110+
/**
111+
* @dataProvider provideGuidEntityClasses
112+
*/
113+
public function testFilterEmptyUuids($entityClass)
114+
{
115+
$em = DoctrineTestHelper::createTestEntityManager();
116+
117+
$query = $this->getMockBuilder('QueryMock')
118+
->setMethods(array('setParameter', 'getResult', 'getSql', '_doExecute'))
119+
->getMock();
120+
121+
$query->expects($this->once())
122+
->method('setParameter')
123+
->with('ORMQueryBuilderLoader_getEntitiesByIds_id', array('71c5fd46-3f16-4abb-bad7-90ac1e654a2d', 'b98e8e11-2897-44df-ad24-d2627eb7f499'), Connection::PARAM_STR_ARRAY)
124+
->willReturn($query);
125+
126+
$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
127+
->setConstructorArgs(array($em))
128+
->setMethods(array('getQuery'))
129+
->getMock();
130+
131+
$qb->expects($this->once())
132+
->method('getQuery')
133+
->willReturn($query);
134+
135+
$qb->select('e')
136+
->from($entityClass, 'e');
137+
138+
$loader = new ORMQueryBuilderLoader($qb);
139+
$loader->getEntitiesByIds('id', array('71c5fd46-3f16-4abb-bad7-90ac1e654a2d', '', 'b98e8e11-2897-44df-ad24-d2627eb7f499'));
140+
}
141+
110142
public function testEmbeddedIdentifierName()
111143
{
112144
if (Version::compare('2.5.0') > 0) {
@@ -140,4 +172,12 @@ public function testEmbeddedIdentifierName()
140172
$loader = new ORMQueryBuilderLoader($qb);
141173
$loader->getEntitiesByIds('id.value', array(1, '', 2, 3, 'foo'));
142174
}
175+
176+
public function provideGuidEntityClasses()
177+
{
178+
return array(
179+
array('Symfony\Bridge\Doctrine\Tests\Fixtures\GuidIdEntity'),
180+
array('Symfony\Bridge\Doctrine\Tests\Fixtures\UuidIdEntity'),
181+
);
182+
}
143183
}

0 commit comments

Comments
 (0)