Skip to content

Commit e318e54

Browse files
authored
Merge pull request #727 from utopia-php/feat-relationship-updates
Add nested spatial query tests
2 parents 1e3f40b + 584761d commit e318e54

File tree

2 files changed

+677
-448
lines changed

2 files changed

+677
-448
lines changed

src/Database/Database.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3859,7 +3859,7 @@ private function populateOneToManyRelationshipsBatch(
38593859
return [];
38603860
}
38613861

3862-
// For batch relationship population, we need to fetch documents with all fields
3862+
// For batch relationship population, we need to fetch documents with all attributes
38633863
// to enable proper grouping by back-reference, then apply selects afterward
38643864
$selectQueries = [];
38653865
$otherQueries = [];
@@ -4131,30 +4131,30 @@ private function applySelectFiltersToDocuments(array $documents, array $selectQu
41314131
return;
41324132
}
41334133

4134-
// Collect all fields to keep from select queries
4135-
$fieldsToKeep = [];
4134+
// Collect all attributes to keep from select queries
4135+
$attributesToKeep = [];
41364136
foreach ($selectQueries as $selectQuery) {
41374137
foreach ($selectQuery->getValues() as $value) {
4138-
$fieldsToKeep[$value] = true;
4138+
$attributesToKeep[$value] = true;
41394139
}
41404140
}
41414141

41424142
// Early return if wildcard selector present
4143-
if (isset($fieldsToKeep['*'])) {
4143+
if (isset($attributesToKeep['*'])) {
41444144
return;
41454145
}
41464146

41474147
// Always preserve internal attributes (use hashmap for O(1) lookup)
41484148
$internalKeys = \array_map(fn ($attr) => $attr['$id'], $this->getInternalAttributes());
41494149
foreach ($internalKeys as $key) {
4150-
$fieldsToKeep[$key] = true;
4150+
$attributesToKeep[$key] = true;
41514151
}
41524152

41534153
foreach ($documents as $doc) {
41544154
$allKeys = \array_keys($doc->getArrayCopy());
41554155
foreach ($allKeys as $attrKey) {
41564156
// Keep if: explicitly selected OR is internal attribute ($ prefix)
4157-
if (!isset($fieldsToKeep[$attrKey]) && !\str_starts_with($attrKey, '$')) {
4157+
if (!isset($attributesToKeep[$attrKey]) && !\str_starts_with($attrKey, '$')) {
41584158
$doc->removeAttribute($attrKey);
41594159
}
41604160
}
@@ -7640,7 +7640,7 @@ private function processRelationshipQueries(
76407640

76417641
$nestingPath = \implode('.', $nesting);
76427642

7643-
// If nestingPath is empty, it means we want all fields (*) for this relationship
7643+
// If nestingPath is empty, it means we want all attributes (*) for this relationship
76447644
if (empty($nestingPath)) {
76457645
$nestedSelections[$selectedKey][] = Query::select(['*']);
76467646
} else {
@@ -7716,7 +7716,7 @@ private function processNestedRelationshipPath(string $startCollection, array $q
77167716
}
77177717
$pathGroups[$pathKey][] = [
77187718
'method' => $query->getMethod(),
7719-
'field' => \end($parts), // The actual field to query
7719+
'attribute' => \end($parts), // The actual attribute to query
77207720
'values' => $query->getValues(),
77217721
];
77227722
}
@@ -7762,7 +7762,7 @@ private function processNestedRelationshipPath(string $startCollection, array $q
77627762
// Now walk backwards from the deepest collection to the starting collection
77637763
$leafQueries = [];
77647764
foreach ($queryGroup as $q) {
7765-
$leafQueries[] = new Query($q['method'], $q['field'], $q['values']);
7765+
$leafQueries[] = new Query($q['method'], $q['attribute'], $q['values']);
77667766
}
77677767

77687768
// Query the deepest collection
@@ -7864,7 +7864,7 @@ private function processNestedRelationshipPath(string $startCollection, array $q
78647864
* The method works by:
78657865
* 1. Parsing dot-path queries (e.g., "project.employee.company.name")
78667866
* 2. Extracting the first relationship (e.g., "project")
7867-
* 3. If the nested field still contains dots, using iterative processing
7867+
* 3. If the nested attribute still contains dots, using iterative processing
78687868
* 4. Finding matching documents in the related collection
78697869
* 5. Converting to filters on the parent collection
78707870
*
@@ -7914,7 +7914,7 @@ private function convertRelationshipFiltersToSubqueries(
79147914
// Parse the relationship path
79157915
$parts = \explode('.', $attribute);
79167916
$relationshipKey = \array_shift($parts);
7917-
$nestedField = \implode('.', $parts);
7917+
$nestedAttribute = \implode('.', $parts);
79187918
$relationship = $relationshipsByKey[$relationshipKey] ?? null;
79197919

79207920
if (!$relationship) {
@@ -7932,7 +7932,7 @@ private function convertRelationshipFiltersToSubqueries(
79327932

79337933
$groupedQueries[$relationshipKey]['queries'][] = [
79347934
'method' => $method,
7935-
'field' => $nestedField,
7935+
'attribute' => $nestedAttribute,
79367936
'values' => $query->getValues()
79377937
];
79387938

@@ -7951,7 +7951,7 @@ private function convertRelationshipFiltersToSubqueries(
79517951
foreach ($group['queries'] as $queryData) {
79527952
$relatedQueries[] = new Query(
79537953
$queryData['method'],
7954-
$queryData['field'],
7954+
$queryData['attribute'],
79557955
$queryData['values']
79567956
);
79577957
}
@@ -8055,7 +8055,7 @@ private function convertRelationshipFiltersToSubqueries(
80558055
return null;
80568056
}
80578057
} else {
8058-
// For other types, filter by the relationship field
8058+
// For other types, filter by the relationship attribute
80598059
if (!empty($matchingIds)) {
80608060
$additionalQueries[] = Query::equal($relationshipKey, $matchingIds);
80618061
} else {

0 commit comments

Comments
 (0)