Skip to content

Commit 0ef44d8

Browse files
committed
Fix select queries
1 parent ce0bf8f commit 0ef44d8

File tree

2 files changed

+89
-53
lines changed

2 files changed

+89
-53
lines changed

src/Database/Database.php

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4310,7 +4310,7 @@ private function updateDocumentRelationships(Document $collection, Document $old
43104310
}
43114311

43124312
if (\is_string($value)) {
4313-
$related = $this->skipRelationships(fn () => $this->getDocument($relatedCollection->getId(), $value, [Query::select(['$id'])]));
4313+
$related = $this->skipRelationships(fn () => $this->getDocument($relatedCollection->getId(), $value, [Query::select('$id')]));
43144314
if ($related->isEmpty()) {
43154315
// If no such document exists in related collection
43164316
// For one-one we need to update the related key to null if no relation exists
@@ -4339,7 +4339,7 @@ private function updateDocumentRelationships(Document $collection, Document $old
43394339
switch (\gettype($value)) {
43404340
case 'string':
43414341
$related = $this->skipRelationships(
4342-
fn () => $this->getDocument($relatedCollection->getId(), $value, [Query::select(['$id'])])
4342+
fn () => $this->getDocument($relatedCollection->getId(), $value, [Query::select('$id')])
43434343
);
43444344

43454345
if ($related->isEmpty()) {
@@ -4351,7 +4351,7 @@ private function updateDocumentRelationships(Document $collection, Document $old
43514351
if (
43524352
$oldValue?->getId() !== $value
43534353
&& !($this->skipRelationships(fn () => $this->findOne($relatedCollection->getId(), [
4354-
Query::select(['$id']),
4354+
Query::select('$id'),
43554355
Query::equal($twoWayKey, [$value]),
43564356
]))->isEmpty())
43574357
) {
@@ -4372,7 +4372,7 @@ private function updateDocumentRelationships(Document $collection, Document $old
43724372
if (
43734373
$oldValue?->getId() !== $value->getId()
43744374
&& !($this->skipRelationships(fn () => $this->findOne($relatedCollection->getId(), [
4375-
Query::select(['$id']),
4375+
Query::select('$id'),
43764376
Query::equal($twoWayKey, [$value->getId()]),
43774377
]))->isEmpty())
43784378
) {
@@ -4453,7 +4453,7 @@ private function updateDocumentRelationships(Document $collection, Document $old
44534453
foreach ($value as $relation) {
44544454
if (\is_string($relation)) {
44554455
$related = $this->skipRelationships(
4456-
fn () => $this->getDocument($relatedCollection->getId(), $relation, [Query::select(['$id'])])
4456+
fn () => $this->getDocument($relatedCollection->getId(), $relation, [Query::select('$id')])
44574457
);
44584458

44594459
if ($related->isEmpty()) {
@@ -4467,7 +4467,7 @@ private function updateDocumentRelationships(Document $collection, Document $old
44674467
));
44684468
} elseif ($relation instanceof Document) {
44694469
$related = $this->skipRelationships(
4470-
fn () => $this->getDocument($relatedCollection->getId(), $relation->getId(), [Query::select(['$id'])])
4470+
fn () => $this->getDocument($relatedCollection->getId(), $relation->getId(), [Query::select('$id')])
44714471
);
44724472

44734473
if ($related->isEmpty()) {
@@ -4496,7 +4496,7 @@ private function updateDocumentRelationships(Document $collection, Document $old
44964496

44974497
if (\is_string($value)) {
44984498
$related = $this->skipRelationships(
4499-
fn () => $this->getDocument($relatedCollection->getId(), $value, [Query::select(['$id'])])
4499+
fn () => $this->getDocument($relatedCollection->getId(), $value, [Query::select('$id')])
45004500
);
45014501

45024502
if ($related->isEmpty()) {
@@ -4507,7 +4507,7 @@ private function updateDocumentRelationships(Document $collection, Document $old
45074507
$this->purgeCachedDocument($relatedCollection->getId(), $value);
45084508
} elseif ($value instanceof Document) {
45094509
$related = $this->skipRelationships(
4510-
fn () => $this->getDocument($relatedCollection->getId(), $value->getId(), [Query::select(['$id'])])
4510+
fn () => $this->getDocument($relatedCollection->getId(), $value->getId(), [Query::select('$id')])
45114511
);
45124512

45134513
if ($related->isEmpty()) {
@@ -4577,11 +4577,11 @@ private function updateDocumentRelationships(Document $collection, Document $old
45774577

45784578
foreach ($value as $relation) {
45794579
if (\is_string($relation)) {
4580-
if (\in_array($relation, $oldIds) || $this->getDocument($relatedCollection->getId(), $relation, [Query::select(['$id'])])->isEmpty()) {
4580+
if (\in_array($relation, $oldIds) || $this->getDocument($relatedCollection->getId(), $relation, [Query::select('$id')])->isEmpty()) {
45814581
continue;
45824582
}
45834583
} elseif ($relation instanceof Document) {
4584-
$related = $this->getDocument($relatedCollection->getId(), $relation->getId(), [Query::select(['$id'])]);
4584+
$related = $this->getDocument($relatedCollection->getId(), $relation->getId(), [Query::select('$id')]);
45854585

45864586
if ($related->isEmpty()) {
45874587
if (!isset($value['$permissions'])) {
@@ -5203,7 +5203,7 @@ private function deleteRestrict(
52035203
) {
52045204
Authorization::skip(function () use ($document, $relatedCollection, $twoWayKey) {
52055205
$related = $this->findOne($relatedCollection->getId(), [
5206-
Query::select(['$id']),
5206+
Query::select('$id'),
52075207
Query::equal($twoWayKey, [$document->getId()])
52085208
]);
52095209

@@ -5226,7 +5226,7 @@ private function deleteRestrict(
52265226
&& $side === Database::RELATION_SIDE_CHILD
52275227
) {
52285228
$related = Authorization::skip(fn () => $this->findOne($relatedCollection->getId(), [
5229-
Query::select(['$id']),
5229+
Query::select('$id'),
52305230
Query::equal($twoWayKey, [$document->getId()])
52315231
]));
52325232

@@ -5264,14 +5264,14 @@ private function deleteSetNull(Document $collection, Document $relatedCollection
52645264
Authorization::skip(function () use ($document, $value, $relatedCollection, $twoWay, $twoWayKey, $side) {
52655265
if (!$twoWay && $side === Database::RELATION_SIDE_CHILD) {
52665266
$related = $this->findOne($relatedCollection->getId(), [
5267-
Query::select(['$id']),
5267+
Query::select('$id'),
52685268
Query::equal($twoWayKey, [$document->getId()])
52695269
]);
52705270
} else {
52715271
if (empty($value)) {
52725272
return;
52735273
}
5274-
$related = $this->getDocument($relatedCollection->getId(), $value->getId(), [Query::select(['$id'])]);
5274+
$related = $this->getDocument($relatedCollection->getId(), $value->getId(), [Query::select('$id')]);
52755275
}
52765276

52775277
if ($related->isEmpty()) {
@@ -5312,7 +5312,7 @@ private function deleteSetNull(Document $collection, Document $relatedCollection
53125312

53135313
if (!$twoWay) {
53145314
$value = $this->find($relatedCollection->getId(), [
5315-
Query::select(['$id']),
5315+
Query::select('$id'),
53165316
Query::equal($twoWayKey, [$document->getId()]),
53175317
Query::limit(PHP_INT_MAX)
53185318
]);
@@ -5335,7 +5335,7 @@ private function deleteSetNull(Document $collection, Document $relatedCollection
53355335
$junction = $this->getJunctionCollection($collection, $relatedCollection, $side);
53365336

53375337
$junctions = $this->find($junction, [
5338-
Query::select(['$id']),
5338+
Query::select('$id'),
53395339
Query::equal($twoWayKey, [$document->getId()]),
53405340
Query::limit(PHP_INT_MAX)
53415341
]);
@@ -5405,7 +5405,7 @@ private function deleteCascade(Document $collection, Document $relatedCollection
54055405
}
54065406

54075407
$value = $this->find($relatedCollection->getId(), [
5408-
Query::select(['$id']),
5408+
Query::select('$id'),
54095409
Query::equal($twoWayKey, [$document->getId()]),
54105410
Query::limit(PHP_INT_MAX),
54115411
]);
@@ -5426,7 +5426,8 @@ private function deleteCascade(Document $collection, Document $relatedCollection
54265426
$junction = $this->getJunctionCollection($collection, $relatedCollection, $side);
54275427

54285428
$junctions = $this->skipRelationships(fn () => $this->find($junction, [
5429-
Query::select(['$id', $key]),
5429+
Query::select('$id'),
5430+
Query::select($key),
54305431
Query::equal($twoWayKey, [$document->getId()]),
54315432
Query::limit(PHP_INT_MAX)
54325433
]));
@@ -5853,7 +5854,7 @@ public function find(string $collection, array $queries = [], string $forPermiss
58535854

58545855
//$results = $skipAuth ? Authorization::skip($getResults) : $getResults();
58555856

5856-
foreach ($results as &$node) {
5857+
foreach ($results as $index => $node) {
58575858
if ($this->resolveRelationships && (empty($selects) || !empty($nestedSelections))) {
58585859
$node = $this->silent(fn () => $this->populateDocumentRelationships($collection, $node, $nestedSelections));
58595860
}
@@ -5863,22 +5864,22 @@ public function find(string $collection, array $queries = [], string $forPermiss
58635864
if (!$node->isEmpty()) {
58645865
$node->setAttribute('$collection', $collection->getId());
58655866
}
5866-
}
58675867

5868-
unset($query);
5868+
// Remove internal attributes which are not queried
5869+
if (!empty($selects)) {
5870+
$selectedAttributes = array_map(
5871+
fn ($q) => $q->getAttribute(),
5872+
array_filter($selects, fn ($q) => $q->isSystem() === false)
5873+
);
58695874

5870-
// Remove internal attributes which are not queried
5871-
foreach ($queries as $query) {
5872-
if ($query->getMethod() === Query::TYPE_SELECT) {
5873-
$values = $query->getValues();
5874-
foreach ($results as $result) {
5875-
foreach ($this->getInternalAttributes() as $internalAttribute) {
5876-
if (!\in_array($internalAttribute['$id'], $values)) {
5877-
$result->removeAttribute($internalAttribute['$id']);
5878-
}
5875+
foreach ($this->getInternalAttributes() as $internalAttribute) {
5876+
if (!in_array($internalAttribute['$id'], $selectedAttributes, true)) {
5877+
$node->removeAttribute($internalAttribute['$id']);
58795878
}
58805879
}
58815880
}
5881+
5882+
$results[$index] = $node;
58825883
}
58835884

58845885
$this->trigger(self::EVENT_DOCUMENT_FIND, $results);

0 commit comments

Comments
 (0)