@@ -4119,10 +4119,11 @@ public function updateDocument(string $collection, string $id, Document $documen
41194119
41204120 $ skipPermissionsUpdate = ($ originalPermissions === $ currentPermissions );
41214121 }
4122+ $ createdAt = $ document ->getCreatedAt ();
41224123
41234124 $ document = \array_merge ($ old ->getArrayCopy (), $ document ->getArrayCopy ());
41244125 $ document ['$collection ' ] = $ old ->getAttribute ('$collection ' ); // Make sure user doesn't switch collection ID
4125- $ document ['$createdAt ' ] = $ old ->getCreatedAt (); // Make sure user doesn't switch createdAt
4126+ $ document ['$createdAt ' ] = ( $ createdAt === null || ! $ this -> preserveDates ) ? $ old ->getCreatedAt () : $ createdAt ; // Make sure user doesn't switch createdAt
41264127
41274128 if ($ this ->adapter ->getSharedTables ()) {
41284129 $ document ['$tenant ' ] = $ old ->getTenant (); // Make sure user doesn't switch tenant
@@ -4251,7 +4252,7 @@ public function updateDocument(string $collection, string $id, Document $documen
42514252
42524253 if ($ shouldUpdate ) {
42534254 $ updatedAt = $ document ->getUpdatedAt ();
4254- $ document ->setAttribute ('$updatedAt ' , empty ($ updatedAt) || !$ this ->preserveDates ? $ time : $ updatedAt );
4255+ $ document ->setAttribute ('$updatedAt ' , ($ updatedAt === null || !$ this ->preserveDates ) ? $ time : $ updatedAt );
42554256 }
42564257
42574258 // Check if document was updated after the request timestamp
@@ -4365,29 +4366,31 @@ public function updateDocuments(
43654366 if (!empty ($ cursor ) && $ cursor ->getCollection () !== $ collection ->getId ()) {
43664367 throw new DatabaseException ("Cursor document must be from the same Collection. " );
43674368 }
4368-
4369+ $ attributesToCheckForRequiredValidation = [ ' $updatedAt ' ];
43694370 unset($ updates ['$id ' ]);
4370- unset($ updates ['$createdAt ' ]);
43714371 unset($ updates ['$tenant ' ]);
4372-
4372+ if (($ updates ->getCreatedAt () === null || !$ this ->preserveDates )) {
4373+ unset($ updates ['$createdAt ' ]);
4374+ } else {
4375+ $ updates ['$createdAt ' ] = $ updates ->getCreatedAt ();
4376+ $ attributesToCheckForRequiredValidation [] = '$createdAt ' ;
4377+ }
43734378 if ($ this ->adapter ->getSharedTables ()) {
43744379 $ updates ['$tenant ' ] = $ this ->adapter ->getTenant ();
43754380 }
43764381
4377- if (!$ this ->preserveDates ) {
4378- $ updates ['$updatedAt ' ] = DateTime::now ();
4379- }
4382+ $ updatedAt = $ updates ->getUpdatedAt ();
4383+ $ updates ['$updatedAt ' ] = ($ updatedAt === null || !$ this ->preserveDates ) ? DateTime::now () : $ updatedAt ;
43804384
43814385 $ updates = $ this ->encode ($ collection , $ updates );
4382-
43834386 // Check new document structure
43844387 $ validator = new PartialStructure (
43854388 $ collection ,
43864389 $ this ->adapter ->getMinDateTime (),
43874390 $ this ->adapter ->getMaxDateTime (),
43884391 );
43894392
4390- if (!$ validator ->isValid ($ updates )) {
4393+ if (!$ validator ->isValid ($ updates, $ attributesToCheckForRequiredValidation )) {
43914394 throw new StructureException ($ validator ->getDescription ());
43924395 }
43934396
0 commit comments