diff --git a/src/Exceptions/InvalidQueryDateException.php b/src/Exceptions/InvalidQueryDateException.php new file mode 100644 index 0000000000..91566069a5 --- /dev/null +++ b/src/Exceptions/InvalidQueryDateException.php @@ -0,0 +1,7 @@ +handle(); if (($parent = $this->field()->parent()) && $parent instanceof Localization) { $site = $parent->locale(); diff --git a/src/Query/EmptyEntryQueryBuilder.php b/src/Query/EmptyEntryQueryBuilder.php new file mode 100644 index 0000000000..49f58877a3 --- /dev/null +++ b/src/Query/EmptyEntryQueryBuilder.php @@ -0,0 +1,16 @@ +where(fn ($query) => $this ->getCollectionsForStatusQuery() - ->each(fn ($collection) => $query->orWhere(fn ($q) => $this->addCollectionStatusLogicToQuery($q, $status, $collection)))); + ->each(function ($collection) use ($query, $status) { + try { + return $query->orWhere(fn ($q) => $this->addCollectionStatusLogicToQuery($q, $status, $collection)); + } catch (InvalidQueryDateException $e) { + return new EmptyEntryQueryBuilder(); + } + }) + ); } private function addCollectionStatusLogicToQuery($query, $status, $collection): void @@ -35,7 +44,7 @@ private function addCollectionStatusLogicToQuery($query, $status, $collection): if (! $collection->dated() || ($collection->futureDateBehavior() === 'public' && $collection->pastDateBehavior() === 'public')) { if ($status === 'scheduled' || $status === 'expired') { - $query->where('date', 'invalid'); // intentionally trigger no results. + throw new InvalidQueryDateException(); // intentionally trigger no results. } return; @@ -47,7 +56,7 @@ private function addCollectionStatusLogicToQuery($query, $status, $collection): : $query->where('date', '<', now()); if ($status === 'expired') { - $query->where('date', 'invalid'); // intentionally trigger no results. + throw new InvalidQueryDateException(); // intentionally trigger no results. } } @@ -57,7 +66,7 @@ private function addCollectionStatusLogicToQuery($query, $status, $collection): : $query->where('date', '>', now()); if ($status === 'scheduled') { - $query->where('date', 'invalid'); // intentionally trigger no results. + throw new InvalidQueryDateException(); // intentionally trigger no results. } } } diff --git a/tests/Fieldtypes/EntriesTest.php b/tests/Fieldtypes/EntriesTest.php index 7416ca21d0..6407d2a4a0 100644 --- a/tests/Fieldtypes/EntriesTest.php +++ b/tests/Fieldtypes/EntriesTest.php @@ -82,6 +82,11 @@ public function it_augments_to_a_query_builder_when_theres_no_value() $this->assertInstanceOf(Builder::class, $augmented); $this->assertCount(0, $augmented->get()); + + $augmented = $this->fieldtype()->augment([]); + + $this->assertInstanceOf(Builder::class, $augmented); + $this->assertCount(0, $augmented->get()); } #[Test]