diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 599da878e..d222080ce 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -977,9 +977,18 @@ public function updateDocument(Document $collection, string $id, Document $docum $spatialAttributes = $this->getSpatialAttributes($collection); $collection = $collection->getId(); $attributes = $document->getAttributes(); - $attributes['_createdAt'] = $document->getCreatedAt(); - $attributes['_updatedAt'] = $document->getUpdatedAt(); - $attributes['_permissions'] = json_encode($document->getPermissions()); + if ($document->offsetExists('$updatedAt')) { + $attributes['_updatedAt'] = $document->getUpdatedAt(); + } + if ($document->offsetExists('$id')) { + $attributes['_uid'] = $document->getId(); + } + if ($document->offsetExists('$createdAt')) { + $attributes['_createdAt'] = $document->getCreatedAt(); + } + if ($document->offsetExists('$permissions')) { + $attributes['_permissions'] = json_encode($document->getPermissions()); + } $name = $this->filter($collection); $columns = ''; @@ -998,7 +1007,8 @@ public function updateDocument(Document $collection, string $id, Document $docum * Get current permissions from the database */ $sqlPermissions = $this->getPDO()->prepare($sql); - $sqlPermissions->bindValue(':_uid', $document->getId()); + + $sqlPermissions->bindValue(':_uid', $id); if ($this->sharedTables) { $sqlPermissions->bindValue(':_tenant', $this->tenant); @@ -1071,7 +1081,7 @@ public function updateDocument(Document $collection, string $id, Document $docum $removeQuery = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $removeQuery); $stmtRemovePermissions = $this->getPDO()->prepare($removeQuery); - $stmtRemovePermissions->bindValue(':_uid', $document->getId()); + $stmtRemovePermissions->bindValue(':_uid', $id); if ($this->sharedTables) { $stmtRemovePermissions->bindValue(':_tenant', $this->tenant); @@ -1119,7 +1129,8 @@ public function updateDocument(Document $collection, string $id, Document $docum $stmtAddPermissions = $this->getPDO()->prepare($sql); - $stmtAddPermissions->bindValue(":_uid", $document->getId()); + $newUid = $document->offsetExists('$id') ? $document->getId() : $id; + $stmtAddPermissions->bindValue(":_uid", $newUid); if ($this->sharedTables) { $stmtAddPermissions->bindValue(":_tenant", $this->tenant); @@ -1168,7 +1179,7 @@ public function updateDocument(Document $collection, string $id, Document $docum $sql = " UPDATE {$this->getSQLTable($name)} - SET {$columns} _uid = :_newUid + SET " . \rtrim($columns, ',') . " WHERE _id=:_sequence {$this->getTenantQuery($collection)} "; @@ -1178,7 +1189,6 @@ public function updateDocument(Document $collection, string $id, Document $docum $stmt = $this->getPDO()->prepare($sql); $stmt->bindValue(':_sequence', $document->getSequence()); - $stmt->bindValue(':_newUid', $document->getId()); if ($this->sharedTables) { $stmt->bindValue(':_tenant', $this->tenant); diff --git a/src/Database/Database.php b/src/Database/Database.php index 1a3a2adf0..54e5f8231 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -6158,18 +6158,13 @@ public function updateDocument(string $collection, string $id, Document $documen $skipPermissionsUpdate = ($originalPermissions === $currentPermissions); } - $createdAt = $document->getCreatedAt(); - - $document = \array_merge($old->getArrayCopy(), $document->getArrayCopy()); - $document['$collection'] = $old->getAttribute('$collection'); // Make sure user doesn't switch collection ID - $document['$createdAt'] = ($createdAt === null || !$this->preserveDates) ? $old->getCreatedAt() : $createdAt; + if (!$this->preserveDates || $document->getCreatedAt() === null) { + $document->removeAttribute('$createdAt'); + } if ($this->adapter->getSharedTables()) { - $tenant = $old->getTenant(); - $document['$tenant'] = $tenant; - $old->setAttribute('$tenant', $tenant); // Normalize for strict comparison + $old->setAttribute('$tenant', $old->getTenant()); // Normalize for strict comparison } - $document = new Document($document); $attributes = $collection->getAttribute('attributes', []); @@ -6311,10 +6306,10 @@ public function updateDocument(string $collection, string $id, Document $documen throw new ConflictException('Document was updated after the request timestamp'); } - $document = $this->encode($collection, $document); + $document = $this->encode($collection, clone $document, applyDefaults: false); if ($this->validate) { - $structureValidator = new Structure( + $structureValidator = new PartialStructure( $collection, $this->adapter->getIdAttributeType(), $this->adapter->getMinDateTime(), @@ -6334,8 +6329,12 @@ public function updateDocument(string $collection, string $id, Document $documen $document = $this->adapter->castingBefore($collection, $document); + $document->setAttribute('$sequence', $old->getSequence()); + $this->adapter->updateDocument($collection, $id, $document, $skipPermissionsUpdate); + $document = new Document(\array_merge($old->getArrayCopy(), $document->getArrayCopy())); + $document = $this->adapter->castingAfter($collection, $document); $this->purgeCachedDocument($collection->getId(), $id);