Skip to content

Commit 25e3dc8

Browse files
committed
update: check against wildcards; remove: unnecessary filtering.
1 parent 0d4dc51 commit 25e3dc8

1 file changed

Lines changed: 11 additions & 30 deletions

File tree

src/Database/Database.php

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,7 +3078,12 @@ public function getDocument(string $collection, string $id, array $queries = [],
30783078
$document = $this->decode($collection, $document, $selections);
30793079
$this->map = [];
30803080

3081-
if ($this->resolveRelationships && (empty($selects) || !empty($nestedSelections))) {
3081+
$hasWildcards = !empty(array_filter(
3082+
$selects,
3083+
fn ($select) => in_array('*', $select->getValues(), true)
3084+
));
3085+
3086+
if ($this->resolveRelationships && (empty($selects) || $hasWildcards || !empty($nestedSelections))) {
30823087
$document = $this->silent(fn () => $this->populateDocumentRelationships($collection, $document, $nestedSelections));
30833088
}
30843089

@@ -3097,20 +3102,6 @@ public function getDocument(string $collection, string $id, array $queries = [],
30973102
}
30983103
}
30993104

3100-
// Remove internal attributes if not queried for select query
3101-
// $id, $permissions and $collection are the default selected attributes for (MariaDB, MySQL, SQLite, Postgres)
3102-
// All internal attributes are default selected attributes for (MongoDB)
3103-
foreach ($queries as $query) {
3104-
if ($query->getMethod() === Query::TYPE_SELECT) {
3105-
$values = $query->getValues();
3106-
foreach ($this->getInternalAttributes() as $internalAttribute) {
3107-
if (!\in_array($internalAttribute['$id'], $values)) {
3108-
$document->removeAttribute($internalAttribute['$id']);
3109-
}
3110-
}
3111-
}
3112-
}
3113-
31143105
$this->trigger(self::EVENT_DOCUMENT_READ, $document);
31153106

31163107
return $document;
@@ -5791,9 +5782,13 @@ public function find(string $collection, array $queries = [], string $forPermiss
57915782
);
57925783

57935784
$results = $skipAuth ? Authorization::skip($getResults) : $getResults();
5785+
$hasWildcards = !empty(array_filter(
5786+
$selects,
5787+
fn ($select) => in_array('*', $select->getValues(), true)
5788+
));
57945789

57955790
foreach ($results as &$node) {
5796-
if ($this->resolveRelationships && (empty($selects) || !empty($nestedSelections))) {
5791+
if ($this->resolveRelationships && (empty($selects) || $hasWildcards || !empty($nestedSelections))) {
57975792
$node = $this->silent(fn () => $this->populateDocumentRelationships($collection, $node, $nestedSelections));
57985793
}
57995794
$node = $this->casting($collection, $node);
@@ -5806,20 +5801,6 @@ public function find(string $collection, array $queries = [], string $forPermiss
58065801

58075802
unset($query);
58085803

5809-
// Remove internal attributes which are not queried
5810-
foreach ($queries as $query) {
5811-
if ($query->getMethod() === Query::TYPE_SELECT) {
5812-
$values = $query->getValues();
5813-
foreach ($results as $result) {
5814-
foreach ($this->getInternalAttributes() as $internalAttribute) {
5815-
if (!\in_array($internalAttribute['$id'], $values)) {
5816-
$result->removeAttribute($internalAttribute['$id']);
5817-
}
5818-
}
5819-
}
5820-
}
5821-
}
5822-
58235804
$this->trigger(self::EVENT_DOCUMENT_FIND, $results);
58245805

58255806
return $results;

0 commit comments

Comments
 (0)