diff --git a/src/Database/Database.php b/src/Database/Database.php index 3cbd515c1..8397eed12 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -3488,7 +3488,7 @@ public function createDocuments( $time = DateTime::now(); $modified = 0; - foreach ($documents as &$document) { + foreach ($documents as $document) { $createdAt = $document->getCreatedAt(); $updatedAt = $document->getUpdatedAt(); @@ -3529,11 +3529,13 @@ public function createDocuments( return $this->adapter->createDocuments($collection->getId(), $chunk); }); - foreach ($batch as $doc) { + foreach ($batch as $document) { if ($this->resolveRelationships) { - $doc = $this->silent(fn () => $this->populateDocumentRelationships($collection, $doc)); + $document = $this->silent(fn () => $this->populateDocumentRelationships($collection, $document)); } - $onNext && $onNext($doc); + + $document = $this->decode($collection, $document); + $onNext && $onNext($document); $modified++; } } @@ -4142,6 +4144,10 @@ public function updateDocuments( unset($updates['$createdAt']); unset($updates['$tenant']); + if ($this->adapter->getSharedTables()) { + $updates['$tenant'] = $this->adapter->getTenant(); + } + if (!$this->preserveDates) { $updates['$updatedAt'] = DateTime::now(); } @@ -4163,7 +4169,6 @@ public function updateDocuments( $last = $cursor; $modified = 0; - // Resolve and update relationships while (true) { if ($limit && $limit < $batchSize) { $batchSize = $limit; @@ -4190,12 +4195,14 @@ public function updateDocuments( } foreach ($batch as &$document) { + $new = new Document(\array_merge($document->getArrayCopy(), $updates->getArrayCopy())); + if ($this->resolveRelationships) { - $newDocument = new Document(array_merge($document->getArrayCopy(), $updates->getArrayCopy())); - $this->silent(fn () => $this->updateDocumentRelationships($collection, $document, $newDocument)); - $document = $newDocument; + $this->silent(fn () => $this->updateDocumentRelationships($collection, $document, $new)); } + $document = $new; + // Check if document was updated after the request timestamp try { $oldUpdatedAt = new \DateTime($document->getUpdatedAt()); @@ -4206,6 +4213,8 @@ public function updateDocuments( if (!is_null($this->timestamp) && $oldUpdatedAt > $this->timestamp) { throw new ConflictException('Document was updated after the request timestamp'); } + + $document = $this->encode($collection, $document); } $this->withTransaction(function () use ($collection, $updates, $batch) { @@ -4217,14 +4226,8 @@ public function updateDocuments( }); foreach ($batch as $doc) { - if ($this->getSharedTables() && $this->getTenantPerDocument()) { - $this->withTenant($doc->getTenant(), function () use ($collection, $doc) { - $this->purgeCachedDocument($collection->getId(), $doc->getId()); - }); - } else { - $this->purgeCachedDocument($collection->getId(), $doc->getId()); - } - + $this->purgeCachedDocument($collection->getId(), $doc->getId()); + $doc = $this->decode($collection, $doc); $onNext && $onNext($doc); $modified++; } @@ -6105,7 +6108,7 @@ public function decode(Document $collection, Document $document, array $selectio } } - $attributes = array_merge($attributes, $this->getInternalAttributes()); + $attributes = \array_merge($attributes, $this->getInternalAttributes()); foreach ($attributes as $attribute) { $key = $attribute['$id'] ?? ''; @@ -6125,7 +6128,7 @@ public function decode(Document $collection, Document $document, array $selectio $value = (is_null($value)) ? [] : $value; foreach ($value as &$node) { - foreach (array_reverse($filters) as $filter) { + foreach (\array_reverse($filters) as $filter) { $node = $this->decodeAttribute($filter, $node, $document); } } diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 5ade6f8b1..b974e2748 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -2289,13 +2289,15 @@ public function testCreateDocuments(): array $this->assertEquals($count, \count($documents)); foreach ($documents as $document) { + $fresh = static::getDatabase()->getDocument($collection, $document->getId()); + $this->assertEquals($document, $fresh); $this->assertNotEmpty(true, $document->getId()); $this->assertIsString($document->getAttribute('string')); $this->assertEquals('text📝', $document->getAttribute('string')); // Also makes sure an emoji is working $this->assertIsInt($document->getAttribute('integer')); $this->assertEquals(5, $document->getAttribute('integer')); $this->assertIsInt($document->getAttribute('bigint')); - $this->assertEquals(9223372036854775807, $document->getAttribute('bigint')); + $this->assertEquals(Database::BIG_INT_MAX, $document->getAttribute('bigint')); } return $documents; @@ -17600,6 +17602,8 @@ public function testUpdateDocuments(): void $this->assertEquals(5, $count); foreach ($results as $document) { + $fresh = static::getDatabase()->getDocument($collection, $document->getId()); + $this->assertEquals($fresh, $document); $this->assertEquals('text📝 updated', $document->getAttribute('string')); }