Skip to content

Commit 00e5f08

Browse files
authored
Merge pull request #783 from DirectoryTree/bug-782
Bug 782 - ->whereIn() returns all records instead of no records when passed an empty array
2 parents e081b80 + aa58f71 commit 00e5f08

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/Query/Builder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,15 @@ public function whereNotContains(string $field, string $value): static
10631063
*/
10641064
public function whereIn(string $field, array $values): static
10651065
{
1066+
if (empty($values)) {
1067+
// If the array of values is empty, we will
1068+
// add an empty OR filter to the query to
1069+
// ensure that no results are returned.
1070+
$this->rawFilter('(|)');
1071+
1072+
return $this;
1073+
}
1074+
10661075
return $this->orFilter(function (Builder $query) use ($field, $values) {
10671076
foreach ($values as $value) {
10681077
$query->whereEquals($field, $value);

tests/Integration/QueryTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,15 @@ public function test_it_cannot_override_limit_when_chunking()
6464

6565
$this->assertEquals(2, $pages);
6666
}
67+
68+
public function test_it_returns_no_results_with_empty_where_in_array()
69+
{
70+
$user = $this->makeUser($this->ou);
71+
72+
$user->save();
73+
74+
$this->assertCount(1, User::get());
75+
$this->assertCount(1, User::whereIn('cn', [$user->getFirstAttribute('cn')])->get());
76+
$this->assertEmpty(User::whereIn('cn', [])->get());
77+
}
6778
}

tests/Unit/Query/BuilderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,15 @@ public function test_built_where_in()
625625
$this->assertEquals('(|(name=john)(name=mary)(name=sue))', $b->getUnescapedQuery());
626626
}
627627

628+
public function test_built_where_in_with_empty_array()
629+
{
630+
$b = $this->newBuilder();
631+
632+
$b->whereIn('name', []);
633+
634+
$this->assertEquals('(|)', $b->getUnescapedQuery());
635+
}
636+
628637
public function test_built_where_approximately_equals()
629638
{
630639
$b = $this->newBuilder();

0 commit comments

Comments
 (0)