From 5069fcea61e47b383645026391c55aca6f04889b Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 18 Apr 2024 16:23:26 -0400 Subject: [PATCH 1/3] add collections when querying by id --- src/Stache/Query/EntryQueryBuilder.php | 34 +++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Stache/Query/EntryQueryBuilder.php b/src/Stache/Query/EntryQueryBuilder.php index 89d421252c..eb3b5e7ea3 100644 --- a/src/Stache/Query/EntryQueryBuilder.php +++ b/src/Stache/Query/EntryQueryBuilder.php @@ -58,9 +58,7 @@ protected function getItems($keys) protected function getFilteredKeys() { - $collections = empty($this->collections) - ? Facades\Collection::handles() - : $this->collections; + $this->collections = $collections = $this->initCollections(); $this->addTaxonomyWheres(); @@ -69,6 +67,36 @@ protected function getFilteredKeys() : $this->getKeysFromCollectionsWithWheres($collections, $this->wheres); } + private function initCollections(): array + { + // If the collections property isn't empty, it means the user has explicitly + // queried for them. In that case, we'll use them and skip the auto-detection. + if (! empty($this->collections)) { + return $this->collections; + } + + // Otherwise, we'll detect them by looking at where clauses targeting the "id" column. + $ids = collect($this->wheres)->where('column', 'id')->flatMap(fn ($where) => $where['values'] ?? [$where['value']]); + + // If no IDs were queried, fall back to all collections. + if ($ids->isEmpty()) { + return Collection::handles()->all(); + } + + return Collection::handles() + ->flatMap(fn ($collection) => $this->getWhereColumnKeysFromStore($collection, ['column' => 'collectionHandle'])) + ->keys() + ->mapWithKeys(function ($value) { + [$collection, $id] = explode('::', $value); + + return [$id => $collection]; + }) + ->only($ids->all()) + ->unique() + ->values() + ->all(); + } + protected function getKeysFromCollections($collections) { return collect($collections)->flatMap(function ($collection) { From eed9d50fbf893176739052d492c9a9bca48ab8e4 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 18 Apr 2024 16:40:52 -0400 Subject: [PATCH 2/3] blink the map --- src/Stache/Query/EntryQueryBuilder.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Stache/Query/EntryQueryBuilder.php b/src/Stache/Query/EntryQueryBuilder.php index eb3b5e7ea3..69ca47ce3d 100644 --- a/src/Stache/Query/EntryQueryBuilder.php +++ b/src/Stache/Query/EntryQueryBuilder.php @@ -5,6 +5,7 @@ use Statamic\Contracts\Entries\QueryBuilder; use Statamic\Entries\EntryCollection; use Statamic\Facades; +use Statamic\Facades\Blink; use Statamic\Facades\Collection; use Statamic\Support\Arr; @@ -83,18 +84,16 @@ private function initCollections(): array return Collection::handles()->all(); } - return Collection::handles() - ->flatMap(fn ($collection) => $this->getWhereColumnKeysFromStore($collection, ['column' => 'collectionHandle'])) - ->keys() - ->mapWithKeys(function ($value) { - [$collection, $id] = explode('::', $value); - - return [$id => $collection]; - }) - ->only($ids->all()) - ->unique() - ->values() - ->all(); + return Blink::once('entry-to-collection-map', function () { + return Collection::handles() + ->flatMap(fn ($collection) => $this->getWhereColumnKeysFromStore($collection, ['column' => 'collectionHandle'])) + ->keys() + ->mapWithKeys(function ($value) { + [$collection, $id] = explode('::', $value); + + return [$id => $collection]; + }); + })->only($ids->all())->unique()->values()->all(); } protected function getKeysFromCollections($collections) From 5d91e58f705ab057e4c63a3796c57ec861181b07 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 19 Apr 2024 16:27:38 -0400 Subject: [PATCH 3/3] wip --- src/Stache/Query/EntryQueryBuilder.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Stache/Query/EntryQueryBuilder.php b/src/Stache/Query/EntryQueryBuilder.php index 69ca47ce3d..923cecc9e4 100644 --- a/src/Stache/Query/EntryQueryBuilder.php +++ b/src/Stache/Query/EntryQueryBuilder.php @@ -59,7 +59,9 @@ protected function getItems($keys) protected function getFilteredKeys() { - $this->collections = $collections = $this->initCollections(); + $collections = empty($this->collections) + ? Facades\Collection::handles() + : $this->collections; $this->addTaxonomyWheres(); @@ -68,7 +70,12 @@ protected function getFilteredKeys() : $this->getKeysFromCollectionsWithWheres($collections, $this->wheres); } - private function initCollections(): array + private function addCollectionWheres(): void + { + $this->collections = $this->getCollectionWheres(); + } + + private function getCollectionWheres(): array { // If the collections property isn't empty, it means the user has explicitly // queried for them. In that case, we'll use them and skip the auto-detection. @@ -173,6 +180,8 @@ protected function getWhereColumnKeyValuesByIndex($column) public function whereStatus(string $status) { + $this->addCollectionWheres(); + if (! in_array($status, self::STATUSES)) { throw new \Exception("Invalid status [$status]"); }