@@ -5418,71 +5418,71 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
54185418 throw new DatabaseException ("Cursor document must be from the same Collection. " );
54195419 }
54205420
5421- $ documents = $ this ->withTransaction (function () use ($ collection , $ queries , $ batchSize , $ limit , $ cursor , $ skipAuth , $ authorization ) {
5422- $ documents = [];
5423- $ originalLimit = $ limit ;
5424- $ lastDocument = $ cursor ;
5425-
5426- while (true ) {
5427- if ($ limit && $ limit < $ batchSize && $ limit > 0 ) {
5428- $ batchSize = $ limit ;
5429- } elseif (!empty ($ limit )) {
5430- $ limit -= $ batchSize ;
5431- }
5432-
5433- $ new = [
5434- Query::limit ($ batchSize )
5435- ];
5436-
5437- if (! empty ($ lastDocument )) {
5438- $ new [] = Query::cursorAfter ($ lastDocument );
5439- }
5421+ $ documents = [];
5422+ $ originalLimit = $ limit ;
5423+ $ lastDocument = $ cursor ;
5424+
5425+ while (true ) {
5426+ if ($ limit && $ limit < $ batchSize && $ limit > 0 ) {
5427+ $ batchSize = $ limit ;
5428+ } elseif (!empty ($ limit )) {
5429+ $ limit -= $ batchSize ;
5430+ }
54405431
5441- $ affectedDocuments = $ this ->silent (fn () => $ this ->find (
5442- $ collection ->getId (),
5443- array_merge ($ new , $ queries ),
5444- forPermission: Database::PERMISSION_DELETE
5445- ));
5432+ $ new = [
5433+ Query::limit ($ batchSize )
5434+ ];
54465435
5447- if (empty ($ affectedDocuments )) {
5448- break ;
5449- }
5436+ if (! empty ($ lastDocument )) {
5437+ $ new [] = Query:: cursorAfter ( $ lastDocument ) ;
5438+ }
54505439
5451- $ documents = \array_merge ($ affectedDocuments , $ documents );
5440+ $ affectedDocuments = $ this ->silent (fn () => $ this ->find (
5441+ $ collection ->getId (),
5442+ array_merge ($ new , $ queries ),
5443+ forPermission: Database::PERMISSION_DELETE
5444+ ));
54525445
5453- foreach ($ affectedDocuments as $ document ) {
5454- if ($ this ->resolveRelationships ) {
5455- $ document = $ this ->silent (fn () => $ this ->deleteDocumentRelationships (
5456- $ collection ,
5457- $ document
5458- ));
5459- }
5446+ if (empty ($ affectedDocuments )) {
5447+ break ;
5448+ }
54605449
5461- // Check if document was updated after the request timestamp
5462- try {
5463- $ oldUpdatedAt = new \DateTime ($ document ->getUpdatedAt ());
5464- } catch (Exception $ e ) {
5465- throw new DatabaseException ($ e ->getMessage (), $ e ->getCode (), $ e );
5466- }
5450+ $ documents = \array_merge ($ affectedDocuments , $ documents );
54675451
5468- if (!\is_null ($ this ->timestamp ) && $ oldUpdatedAt > $ this ->timestamp ) {
5469- throw new ConflictException ('Document was updated after the request timestamp ' );
5470- }
5452+ foreach ($ affectedDocuments as $ document ) {
5453+ if ($ this ->resolveRelationships ) {
5454+ $ document = $ this ->silent (fn () => $ this ->deleteDocumentRelationships (
5455+ $ collection ,
5456+ $ document
5457+ ));
54715458 }
54725459
5473- if (count ($ affectedDocuments ) < $ batchSize ) {
5474- break ;
5475- } elseif ($ originalLimit && count ($ documents ) == $ originalLimit ) {
5476- break ;
5460+ // Check if document was updated after the request timestamp
5461+ try {
5462+ $ oldUpdatedAt = new \DateTime ($ document ->getUpdatedAt ());
5463+ } catch (Exception $ e ) {
5464+ throw new DatabaseException ($ e ->getMessage (), $ e ->getCode (), $ e );
54775465 }
54785466
5479- $ lastDocument = end ($ affectedDocuments );
5467+ if (!\is_null ($ this ->timestamp ) && $ oldUpdatedAt > $ this ->timestamp ) {
5468+ throw new ConflictException ('Document was updated after the request timestamp ' );
5469+ }
54805470 }
54815471
5482- if (empty ($ documents )) {
5483- return [];
5472+ if (count ($ affectedDocuments ) < $ batchSize ) {
5473+ break ;
5474+ } elseif ($ originalLimit && count ($ documents ) == $ originalLimit ) {
5475+ break ;
54845476 }
54855477
5478+ $ lastDocument = end ($ affectedDocuments );
5479+ }
5480+
5481+ if (empty ($ documents )) {
5482+ return [];
5483+ }
5484+
5485+ $ this ->withTransaction (function () use ($ documents , $ collection , $ batchSize , $ skipAuth , $ authorization ) {
54865486 foreach (\array_chunk ($ documents , $ batchSize ) as $ chunk ) {
54875487 $ getResults = fn () => $ this ->adapter ->deleteDocuments (
54885488 $ collection ->getId (),
@@ -5491,8 +5491,6 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
54915491
54925492 $ skipAuth ? $ authorization ->skip ($ getResults ) : $ getResults ();
54935493 }
5494-
5495- return $ documents ;
54965494 });
54975495
54985496 foreach ($ documents as $ document ) {
0 commit comments