Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix update calls not returning relationships that weren't updated #309

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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 */
Expand All @@ -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;
Expand Down