diff --git a/src/Fieldtypes/Entries.php b/src/Fieldtypes/Entries.php index e2b84d24a39..4bcff6dc032 100644 --- a/src/Fieldtypes/Entries.php +++ b/src/Fieldtypes/Entries.php @@ -519,6 +519,11 @@ public function getColumns() protected function getItemsForPreProcessIndex($values): SupportCollection { + // Nothing selected, so there's nothing to go looking for. + if (empty(Arr::wrap($values))) { + return $this->collect([]); + } + // These are the entries a query for them in any status would find, so they aren't looked for a second time. return Entry::applySubstitutions($this->entriesInTheirSite($values)->unique->id()->values()); } diff --git a/src/Stache/Query/Builder.php b/src/Stache/Query/Builder.php index 59cca5247a4..346f32b6305 100644 --- a/src/Stache/Query/Builder.php +++ b/src/Stache/Query/Builder.php @@ -191,6 +191,11 @@ private function comparesEqualityAsStrings($where) protected function filterWhereIn($values, $where) { + // Nothing can be in an empty list. + if (empty($where['values'])) { + return $values->take(0); + } + $lookup = array_flip(array_map($this->normalizeLookupValue(...), $where['values'])); return $values->filter( diff --git a/tests/Data/Entries/EntryQueryBuilderTest.php b/tests/Data/Entries/EntryQueryBuilderTest.php index c194050a38f..5f2b8677afc 100644 --- a/tests/Data/Entries/EntryQueryBuilderTest.php +++ b/tests/Data/Entries/EntryQueryBuilderTest.php @@ -77,6 +77,17 @@ public function entries_are_found_using_or_where_in() $this->assertEquals(['Post 1', 'Post 2', 'Post 4', 'Post 5'], $entries->map->title->all()); } + #[Test] + public function nothing_is_found_in_an_empty_list() + { + $this->createDummyCollectionAndEntries(); + + $this->assertSame([], Entry::query()->whereIn('id', [])->get()->map->id()->all()); + $this->assertSame([], Entry::query()->whereIn('title', [])->get()->map->id()->all()); + $this->assertSame(['id-1'], Entry::query()->where('title', 'Post 1')->orWhereIn('id', [])->get()->map->id()->all()); + $this->assertSame(['id-1', 'id-2', 'id-3'], Entry::query()->whereNotIn('id', [])->get()->map->id()->all()); + } + #[Test] public function entries_are_ordered_the_way_a_data_collection_sorts_their_values() { diff --git a/tests/Fieldtypes/EntriesTest.php b/tests/Fieldtypes/EntriesTest.php index 9a3885b6c76..168d0a15685 100644 --- a/tests/Fieldtypes/EntriesTest.php +++ b/tests/Fieldtypes/EntriesTest.php @@ -134,6 +134,17 @@ public function it_returns_empty_collection_when_preprocessing_index_and_max_ite $this->assertEquals([], $preProcessed->all()); } + #[Test] + public function it_returns_an_empty_collection_when_preprocessing_index_and_nothing_is_selected() + { + foreach ([null, []] as $value) { + $preProcessed = $this->fieldtype()->preProcessIndex($value); + + $this->assertInstanceOf(Collection::class, $preProcessed); + $this->assertCount(0, $preProcessed); + } + } + #[Test] public function it_includes_drafts_when_pre_processing_for_index() {