Skip to content

Commit 5d285c8

Browse files
author
Elliot Bruneel
committed
fix: handling of empty array in SQL condition generation
1 parent fe5ee70 commit 5d285c8

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/Persisters/Entity/BasicEntityPersister.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,11 @@ public function getSelectConditionStatementSQL($field, $value, $assoc = null, $c
17531753
$value = [$value];
17541754
}
17551755

1756+
if (empty($value)) {
1757+
$selectedColumns[] = $column . ' IN (NULL)';
1758+
continue;
1759+
}
1760+
17561761
$nullKeys = array_keys($value, null, true);
17571762
$nonNullValues = array_diff_key($value, array_flip($nullKeys));
17581763

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional\Ticket;
6+
7+
use Doctrine\ORM\Mapping\Column;
8+
use Doctrine\ORM\Mapping\Entity;
9+
use Doctrine\ORM\Mapping\GeneratedValue;
10+
use Doctrine\ORM\Mapping\Id;
11+
use Doctrine\Tests\OrmFunctionalTestCase;
12+
13+
class GH12254Test extends OrmFunctionalTestCase
14+
{
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->setUpEntitySchema([
20+
GH12254EntityA::class,
21+
]);
22+
23+
$this->_em->persist(new GH12254EntityA());
24+
$this->_em->flush();
25+
$this->_em->clear();
26+
}
27+
28+
public function testFindByEmptyArrayShouldReturnEmptyArray(): void
29+
{
30+
// pretend we are starting afresh
31+
$this->_em = $this->getEntityManager();
32+
$result = $this->_em->getRepository(GH12254EntityA::class)->findBy(['id' => []]);
33+
$this->assertEmpty($result);
34+
}
35+
}
36+
37+
/**
38+
* @Entity()
39+
*/
40+
class GH12254EntityA
41+
{
42+
/**
43+
* @Column(type="integer")
44+
* @Id()
45+
* @GeneratedValue(strategy="AUTO")
46+
* @var int
47+
*/
48+
public $id;
49+
}

tests/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ public function testSelectConditionStatementWithMultipleValuesContainingNull():
153153
);
154154
}
155155

156+
/** @group GH12254 */
157+
public function testSelectConditionStatementWithValuesIsEmptyArray(): void
158+
{
159+
self::assertEquals(
160+
't0.id IN (NULL)',
161+
$this->persister->getSelectConditionStatementSQL('id', [])
162+
);
163+
}
164+
156165
public function testCountCondition(): void
157166
{
158167
$persister = new BasicEntityPersister($this->entityManager, $this->entityManager->getClassMetadata(NonAlphaColumnsEntity::class));

0 commit comments

Comments
 (0)