Skip to content

Commit 6001f9f

Browse files
jasonvargahastinbe
andauthored
[5.x] Fix whereNotIn error with nulls (#13266)
Co-authored-by: Beau Hastings <[email protected]>
1 parent 566eb8b commit 6001f9f

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/Stache/Query/Builder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,19 @@ protected function filterWhereBasic($values, $where)
157157

158158
protected function filterWhereIn($values, $where)
159159
{
160-
$lookup = array_flip($where['values']);
160+
$lookup = array_flip(array_map(fn ($v) => $v ?? '__NULL__', $where['values']));
161161

162162
return $values->filter(
163-
fn ($value) => isset($lookup[$value])
163+
fn ($value) => isset($lookup[$value ?? '__NULL__'])
164164
);
165165
}
166166

167167
protected function filterWhereNotIn($values, $where)
168168
{
169-
$lookup = array_flip($where['values']);
169+
$lookup = array_flip(array_map(fn ($v) => $v ?? '__NULL__', $where['values']));
170170

171171
return $values->filter(
172-
fn ($value) => ! isset($lookup[$value])
172+
fn ($value) => ! isset($lookup[$value ?? '__NULL__'])
173173
);
174174
}
175175

tests/Data/Entries/EntryQueryBuilderTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,36 @@ public function entries_are_found_using_or_where_not_in()
9191
$this->assertEquals(['Post 3', 'Post 4'], $entries->map->title->all());
9292
}
9393

94+
#[Test]
95+
public function entries_are_found_using_where_in_with_null()
96+
{
97+
EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'category' => 'news'])->create();
98+
EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'category' => 'blog'])->create();
99+
EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3'])->create(); // category is null
100+
EntryFactory::id('4')->slug('post-4')->collection('posts')->data(['title' => 'Post 4', 'category' => 'news'])->create();
101+
EntryFactory::id('5')->slug('post-5')->collection('posts')->data(['title' => 'Post 5'])->create(); // category is null
102+
103+
$entries = Entry::query()->whereIn('category', ['news', null])->get();
104+
105+
$this->assertCount(4, $entries);
106+
$this->assertEquals(['Post 1', 'Post 3', 'Post 4', 'Post 5'], $entries->map->title->all());
107+
}
108+
109+
#[Test]
110+
public function entries_are_found_using_where_not_in_with_null()
111+
{
112+
EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'category' => 'news'])->create();
113+
EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'category' => 'blog'])->create();
114+
EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3'])->create(); // category is null
115+
EntryFactory::id('4')->slug('post-4')->collection('posts')->data(['title' => 'Post 4', 'category' => 'news'])->create();
116+
EntryFactory::id('5')->slug('post-5')->collection('posts')->data(['title' => 'Post 5'])->create(); // category is null
117+
118+
$entries = Entry::query()->whereNotIn('category', ['news', null])->get();
119+
120+
$this->assertCount(1, $entries);
121+
$this->assertEquals(['Post 2'], $entries->map->title->all());
122+
}
123+
94124
#[Test]
95125
public function entries_are_found_using_where_date()
96126
{

0 commit comments

Comments
 (0)