@@ -4165,78 +4165,76 @@ public function updateDocuments(string $collection, Document $updates, array $qu
41654165 throw new StructureException ($ validator ->getDescription ());
41664166 }
41674167
4168- $ documents = $ this ->withTransaction (function () use ($ collection , $ queries , $ batchSize , $ updates , $ limit , $ cursor , $ authorization , $ skipAuth ) {
4169- $ documents = [];
4170- $ originalLimit = $ limit ;
4171- $ lastDocument = $ cursor ;
4172-
4173- // Resolve and update relationships
4174- while (true ) {
4175- if ($ limit && $ limit < $ batchSize ) {
4176- $ batchSize = $ limit ;
4177- } elseif (!empty ($ limit )) {
4178- $ limit -= $ batchSize ;
4179- }
4168+ $ documents = [];
4169+ $ originalLimit = $ limit ;
4170+ $ lastDocument = $ cursor ;
41804171
4181- $ new = [
4182- Query::limit ($ batchSize )
4183- ];
4172+ // Resolve and update relationships
4173+ while (true ) {
4174+ if ($ limit && $ limit < $ batchSize ) {
4175+ $ batchSize = $ limit ;
4176+ } elseif (!empty ($ limit )) {
4177+ $ limit -= $ batchSize ;
4178+ }
41844179
4185- if (! empty ( $ lastDocument )) {
4186- $ new [] = Query::cursorAfter ( $ lastDocument );
4187- }
4180+ $ new = [
4181+ Query::limit ( $ batchSize )
4182+ ];
41884183
4189- $ affectedDocuments = $ this ->silent (fn () => $ this ->find (
4190- $ collection ->getId (),
4191- array_merge ($ new , $ queries ),
4192- forPermission: Database::PERMISSION_UPDATE
4193- ));
4184+ if (! empty ($ lastDocument )) {
4185+ $ new [] = Query::cursorAfter ($ lastDocument );
4186+ }
41944187
4195- if (empty ($ affectedDocuments )) {
4196- break ;
4197- }
4188+ $ affectedDocuments = $ this ->silent (fn () => $ this ->find (
4189+ $ collection ->getId (),
4190+ array_merge ($ new , $ queries ),
4191+ forPermission: Database::PERMISSION_UPDATE
4192+ ));
41984193
4199- foreach ($ affectedDocuments as $ document ) {
4200- if ($ this ->resolveRelationships ) {
4201- $ newDocument = new Document (array_merge ($ document ->getArrayCopy (), $ updates ->getArrayCopy ()));
4202- $ this ->silent (fn () => $ this ->updateDocumentRelationships ($ collection , $ document , $ newDocument ));
4203- $ documents [] = $ newDocument ;
4204- }
4194+ if (empty ($ affectedDocuments )) {
4195+ break ;
4196+ }
42054197
4206- // Check if document was updated after the request timestamp
4207- try {
4208- $ oldUpdatedAt = new \ DateTime ( $ document ->getUpdatedAt ( ));
4209- } catch ( Exception $ e ) {
4210- throw new DatabaseException ( $ e -> getMessage (), $ e -> getCode (), $ e ) ;
4211- }
4198+ foreach ( $ affectedDocuments as $ document) {
4199+ if ( $ this -> resolveRelationships ) {
4200+ $ newDocument = new Document ( array_merge ( $ document ->getArrayCopy (), $ updates -> getArrayCopy () ));
4201+ $ this -> silent ( fn () => $ this -> updateDocumentRelationships ( $ collection , $ document , $ newDocument ));
4202+ $ documents [] = $ newDocument ;
4203+ }
42124204
4213- if (!is_null ($ this ->timestamp ) && $ oldUpdatedAt > $ this ->timestamp ) {
4214- throw new ConflictException ('Document was updated after the request timestamp ' );
4215- }
4205+ // Check if document was updated after the request timestamp
4206+ try {
4207+ $ oldUpdatedAt = new \DateTime ($ document ->getUpdatedAt ());
4208+ } catch (Exception $ e ) {
4209+ throw new DatabaseException ($ e ->getMessage (), $ e ->getCode (), $ e );
42164210 }
42174211
4212+ if (!is_null ($ this ->timestamp ) && $ oldUpdatedAt > $ this ->timestamp ) {
4213+ throw new ConflictException ('Document was updated after the request timestamp ' );
4214+ }
4215+ }
4216+
4217+ $ this ->withTransaction (function () use ($ collection , $ updates , $ authorization , $ skipAuth , $ affectedDocuments ) {
42184218 $ getResults = fn () => $ this ->adapter ->updateDocuments (
42194219 $ collection ->getId (),
42204220 $ updates ,
42214221 $ affectedDocuments
42224222 );
42234223
42244224 $ skipAuth ? $ authorization ->skip ($ getResults ) : $ getResults ();
4225+ });
42254226
4226- if (count ($ affectedDocuments ) < $ batchSize ) {
4227- break ;
4228- } elseif ($ originalLimit && count ($ documents ) == $ originalLimit ) {
4229- break ;
4230- }
4231-
4232- $ lastDocument = end ($ affectedDocuments );
4227+ foreach ($ documents as $ document ) {
4228+ $ this ->purgeCachedDocument ($ collection ->getId (), $ document ->getId ());
42334229 }
42344230
4235- return $ documents ;
4236- });
4231+ if (count ($ affectedDocuments ) < $ batchSize ) {
4232+ break ;
4233+ } elseif ($ originalLimit && count ($ documents ) == $ originalLimit ) {
4234+ break ;
4235+ }
42374236
4238- foreach ($ documents as $ document ) {
4239- $ this ->purgeCachedDocument ($ collection ->getId (), $ document ->getId ());
4237+ $ lastDocument = end ($ affectedDocuments );
42404238 }
42414239
42424240 $ this ->trigger (self ::EVENT_DOCUMENTS_UPDATE , new Document ([
0 commit comments