From ed1989bcb16f210bbbb6934402d66be92d072658 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 16 Aug 2023 20:47:17 -0400 Subject: [PATCH] Fix update calls not returning relationships that weren't updated --- src/Database/Database.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 6524abee6..70f3fdc20 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2941,9 +2941,10 @@ public function updateDocument(string $collection, string $id, Document $documen // Skip the nested documents as they will be checked later in recursions. if (\array_key_exists($key, $relationships)) { // No need to compare nested documents more than max depth. - if (count($this->relationshipWriteStack) >= Database::RELATION_MAX_DEPTH - 1) { + if (\count($this->relationshipWriteStack) >= Database::RELATION_MAX_DEPTH - 1) { continue; } + $relationType = (string) $relationships[$key]['options']['relationType']; $side = (string) $relationships[$key]['options']['side']; @@ -3043,7 +3044,7 @@ public function updateDocument(string $collection, string $id, Document $documen $document = $this->silent(fn () => $this->updateDocumentRelationships($collection, $old, $document)); } - $this->adapter->updateDocument($collection->getId(), $document); + $document = $this->adapter->updateDocument($collection->getId(), $document); if ($this->resolveRelationships) { $document = $this->silent(fn () => $this->populateDocumentRelationships($collection, $document)); @@ -3074,13 +3075,17 @@ public function updateDocument(string $collection, string $id, Document $documen */ private function updateDocumentRelationships(Document $collection, Document $old, Document $document): Document { + if ($collection->getId() === self::METADATA) { + return $document; + } + $attributes = $collection->getAttribute('attributes', []); $relationships = \array_filter($attributes, function ($attribute) { return $attribute['type'] === Database::VAR_RELATIONSHIP; }); - $stackCount = count($this->relationshipWriteStack); + $stackCount = \count($this->relationshipWriteStack); foreach ($relationships as $index => $relationship) { /** @var string $key */ @@ -3093,11 +3098,6 @@ private function updateDocumentRelationships(Document $collection, Document $old $twoWayKey = (string) $relationship['options']['twoWayKey']; $side = (string) $relationship['options']['side']; - if ($oldValue == $value) { - $document->removeAttribute($key); - continue; - } - if ($stackCount >= Database::RELATION_MAX_DEPTH - 1 && $this->relationshipWriteStack[$stackCount - 1] !== $relatedCollection->getId()) { $document->removeAttribute($key); continue;