Skip to content

Commit 9b99958

Browse files
committed
Fix yield loops
1 parent 11b0af0 commit 9b99958

1 file changed

Lines changed: 18 additions & 24 deletions

File tree

src/Database/Database.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3493,7 +3493,7 @@ public function createDocuments(
34933493
yield from $this->withTransaction(function () use ($collection, $chunk) {
34943494
$batch = $this->adapter->createDocuments($collection->getId(), $chunk);
34953495

3496-
foreach ($batch as $key => $document) {
3496+
foreach ($batch as $document) {
34973497
if ($this->resolveRelationships) {
34983498
$document = $this->silent(fn () => $this->populateDocumentRelationships($collection, $document));
34993499
}
@@ -4047,6 +4047,7 @@ public function updateDocument(string $collection, string $id, Document $documen
40474047
*
40484048
* @throws AuthorizationException
40494049
* @throws DatabaseException
4050+
* @throws \Throwable
40504051
*/
40514052
public function updateDocuments(
40524053
string $collection,
@@ -4066,7 +4067,6 @@ public function updateDocuments(
40664067
}
40674068

40684069
$documentSecurity = $collection->getAttribute('documentSecurity', false);
4069-
40704070
$authorization = new Authorization(self::PERMISSION_UPDATE);
40714071
$skipAuth = $authorization->isValid($collection->getUpdate());
40724072

@@ -4124,7 +4124,6 @@ public function updateDocuments(
41244124
$originalLimit = $limit;
41254125
$lastDocument = $cursor;
41264126

4127-
// Resolve and update relationships
41284127
while (true) {
41294128
if ($limit && $limit < $batchSize) {
41304129
$batchSize = $limit;
@@ -4136,7 +4135,7 @@ public function updateDocuments(
41364135
Query::limit($batchSize)
41374136
];
41384137

4139-
if (! empty($lastDocument)) {
4138+
if (!empty($lastDocument)) {
41404139
$new[] = Query::cursorAfter($lastDocument);
41414140
}
41424141

@@ -4151,11 +4150,11 @@ public function updateDocuments(
41514150
}
41524151

41534152
$this->withTransaction(function () use ($collection, $updates, $affectedDocuments) {
4154-
foreach ($affectedDocuments as $document) {
4155-
$newDocument = new Document(\array_merge($document->getArrayCopy(), $updates->getArrayCopy()));
4153+
foreach ($affectedDocuments as &$old) {
4154+
$document = new Document(\array_merge($old->getArrayCopy(), $updates->getArrayCopy()));
41564155

41574156
if ($this->resolveRelationships) {
4158-
$this->silent(fn () => $this->updateDocumentRelationships($collection, $document, $newDocument));
4157+
$this->silent(fn () => $this->updateDocumentRelationships($collection, $old, $document));
41594158
}
41604159

41614160
// Check if document was updated after the request timestamp
@@ -4165,9 +4164,11 @@ public function updateDocuments(
41654164
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
41664165
}
41674166

4168-
if (!is_null($this->timestamp) && $oldUpdatedAt > $this->timestamp) {
4167+
if (!\is_null($this->timestamp) && $oldUpdatedAt > $this->timestamp) {
41694168
throw new ConflictException('Document was updated after the request timestamp');
41704169
}
4170+
4171+
$old = $document;
41714172
}
41724173
});
41734174

@@ -4178,21 +4179,17 @@ public function updateDocuments(
41784179
$affectedDocuments
41794180
);
41804181

4181-
/**
4182-
* @var array<Document> $batch
4183-
*/
4184-
$batch = $skipAuth
4182+
$skipAuth
41854183
? $authorization->skip($getResults)
41864184
: $getResults();
41874185

4188-
foreach ($batch as $document) {
4186+
foreach ($affectedDocuments as $document) {
41894187
$this->purgeCachedDocument($collection->getId(), $document->getId());
41904188

41914189
yield $document;
41924190
}
41934191

4194-
4195-
$modified += \count($batch);
4192+
$modified += \count($affectedDocuments);
41964193
});
41974194

41984195
if (\count($affectedDocuments) < $batchSize) {
@@ -5471,11 +5468,11 @@ public function deleteDocuments(
54715468
$internalIds = [];
54725469
$permissionIds = [];
54735470

5474-
$this->withTransaction(function () use ($collection, $affectedDocuments) {
5471+
$this->withTransaction(function () use ($collection, $affectedDocuments, &$internalIds, &$permissionIds) {
54755472
foreach ($affectedDocuments as $document) {
54765473
$internalIds[] = $document->getInternalId();
54775474

5478-
if (!empty($document->getPermissions())) {
5475+
if(!empty($document->getPermissions())) {
54795476
$permissionIds[] = $document->getId();
54805477
}
54815478

@@ -5499,27 +5496,24 @@ public function deleteDocuments(
54995496
}
55005497
});
55015498

5502-
yield from $this->withTransaction(function () use ($collection, $skipAuth, $authorization, $internalIds, $permissionIds, &$modified): \Generator {
5499+
yield from $this->withTransaction(function () use ($collection, $affectedDocuments, $skipAuth, $authorization, $internalIds, $permissionIds, &$modified): \Generator {
55035500
$getResults = fn () => $this->adapter->deleteDocuments(
55045501
$collection->getId(),
55055502
$internalIds,
55065503
$permissionIds
55075504
);
55085505

5509-
/**
5510-
* @var array<Document> $batch
5511-
*/
5512-
$batch = $skipAuth
5506+
$skipAuth
55135507
? $authorization->skip($getResults)
55145508
: $getResults();
55155509

5516-
foreach ($batch as $document) {
5510+
foreach ($affectedDocuments as $document) {
55175511
$this->purgeCachedDocument($collection->getId(), $document->getId());
55185512

55195513
yield $document;
55205514
}
55215515

5522-
$modified += \count($batch);
5516+
$modified += \count($affectedDocuments);
55235517
});
55245518

55255519
if (\count($affectedDocuments) < $batchSize) {

0 commit comments

Comments
 (0)