@@ -335,6 +335,8 @@ class Database
335335
336336 protected bool $ resolveRelationships = true ;
337337
338+ protected bool $ ignoreNestedQueries = false ;
339+
338340 protected bool $ checkRelationshipsExist = true ;
339341
340342 protected int $ relationshipFetchDepth = 1 ;
@@ -547,6 +549,25 @@ public function skipRelationships(callable $callback): mixed
547549 }
548550 }
549551
552+ /**
553+ * Ignore the nested `Query::select` for relationships and return all the related documents.
554+ *
555+ * @template T
556+ * @param callable(): T $callback
557+ * @return T
558+ */
559+ public function ignoreNestedQueries (callable $ callback ): mixed
560+ {
561+ $ previous = $ this ->ignoreNestedQueries ;
562+ $ this ->ignoreNestedQueries = true ;
563+
564+ try {
565+ return $ callback ();
566+ } finally {
567+ $ this ->ignoreNestedQueries = $ previous ;
568+ }
569+ }
570+
550571 public function skipRelationshipsExistCheck (callable $ callback ): mixed
551572 {
552573 $ previous = $ this ->checkRelationshipsExist ;
@@ -3074,7 +3095,7 @@ public function getDocument(string $collection, string $id, array $queries = [],
30743095 $ document = $ this ->decode ($ collection , $ document , $ selections );
30753096 $ this ->map = [];
30763097
3077- if ($ this ->resolveRelationships && Query::hasNestedSelect ($ originalQueries )) {
3098+ if ($ this ->resolveRelationships && ( $ this -> ignoreNestedQueries || Query::hasNestedSelect ($ originalQueries) )) {
30783099 $ document = $ this ->silent (fn () => $ this ->populateDocumentRelationships ($ collection , $ document , $ originalQueries ));
30793100 }
30803101
@@ -3133,7 +3154,8 @@ private function populateDocumentRelationships(Document $collection, Document $d
31333154 // Only continue if this relationship is requested!
31343155 $ selects = Query::filterSelectsByPrefix ($ queries , $ key );
31353156
3136- if (empty ($ selects )) {
3157+ // Skip resolving if respecting nested queries AND no fields were selected
3158+ if (! $ this ->ignoreNestedQueries && empty ($ selects )) {
31373159 $ document ->removeAttribute ($ key );
31383160 continue ;
31393161 }
0 commit comments