@@ -3488,7 +3488,7 @@ public function createDocuments(
34883488 $ time = DateTime::now ();
34893489 $ modified = 0 ;
34903490
3491- foreach ($ documents as & $ document ) {
3491+ foreach ($ documents as $ document ) {
34923492 $ createdAt = $ document ->getCreatedAt ();
34933493 $ updatedAt = $ document ->getUpdatedAt ();
34943494
@@ -3529,11 +3529,13 @@ public function createDocuments(
35293529 return $ this ->adapter ->createDocuments ($ collection ->getId (), $ chunk );
35303530 });
35313531
3532- foreach ($ batch as $ doc ) {
3532+ foreach ($ batch as $ document ) {
35333533 if ($ this ->resolveRelationships ) {
3534- $ doc = $ this ->silent (fn () => $ this ->populateDocumentRelationships ($ collection , $ doc ));
3534+ $ document = $ this ->silent (fn () => $ this ->populateDocumentRelationships ($ collection , $ document ));
35353535 }
3536- $ onNext && $ onNext ($ doc );
3536+
3537+ $ document = $ this ->decode ($ collection , $ document );
3538+ $ onNext && $ onNext ($ document );
35373539 $ modified ++;
35383540 }
35393541 }
@@ -4142,6 +4144,10 @@ public function updateDocuments(
41424144 unset($ updates ['$createdAt ' ]);
41434145 unset($ updates ['$tenant ' ]);
41444146
4147+ if ($ this ->adapter ->getSharedTables ()) {
4148+ $ updates ['$tenant ' ] = $ this ->adapter ->getTenant ();
4149+ }
4150+
41454151 if (!$ this ->preserveDates ) {
41464152 $ updates ['$updatedAt ' ] = DateTime::now ();
41474153 }
@@ -4163,7 +4169,6 @@ public function updateDocuments(
41634169 $ last = $ cursor ;
41644170 $ modified = 0 ;
41654171
4166- // Resolve and update relationships
41674172 while (true ) {
41684173 if ($ limit && $ limit < $ batchSize ) {
41694174 $ batchSize = $ limit ;
@@ -4190,12 +4195,14 @@ public function updateDocuments(
41904195 }
41914196
41924197 foreach ($ batch as &$ document ) {
4198+ $ new = new Document (\array_merge ($ document ->getArrayCopy (), $ updates ->getArrayCopy ()));
4199+
41934200 if ($ this ->resolveRelationships ) {
4194- $ newDocument = new Document (array_merge ($ document ->getArrayCopy (), $ updates ->getArrayCopy ()));
4195- $ this ->silent (fn () => $ this ->updateDocumentRelationships ($ collection , $ document , $ newDocument ));
4196- $ document = $ newDocument ;
4201+ $ this ->silent (fn () => $ this ->updateDocumentRelationships ($ collection , $ document , $ new ));
41974202 }
41984203
4204+ $ document = $ new ;
4205+
41994206 // Check if document was updated after the request timestamp
42004207 try {
42014208 $ oldUpdatedAt = new \DateTime ($ document ->getUpdatedAt ());
@@ -4206,6 +4213,8 @@ public function updateDocuments(
42064213 if (!is_null ($ this ->timestamp ) && $ oldUpdatedAt > $ this ->timestamp ) {
42074214 throw new ConflictException ('Document was updated after the request timestamp ' );
42084215 }
4216+
4217+ $ document = $ this ->encode ($ collection , $ document );
42094218 }
42104219
42114220 $ this ->withTransaction (function () use ($ collection , $ updates , $ batch ) {
@@ -4217,14 +4226,8 @@ public function updateDocuments(
42174226 });
42184227
42194228 foreach ($ batch as $ doc ) {
4220- if ($ this ->getSharedTables () && $ this ->getTenantPerDocument ()) {
4221- $ this ->withTenant ($ doc ->getTenant (), function () use ($ collection , $ doc ) {
4222- $ this ->purgeCachedDocument ($ collection ->getId (), $ doc ->getId ());
4223- });
4224- } else {
4225- $ this ->purgeCachedDocument ($ collection ->getId (), $ doc ->getId ());
4226- }
4227-
4229+ $ this ->purgeCachedDocument ($ collection ->getId (), $ doc ->getId ());
4230+ $ doc = $ this ->decode ($ collection , $ doc );
42284231 $ onNext && $ onNext ($ doc );
42294232 $ modified ++;
42304233 }
@@ -6105,7 +6108,7 @@ public function decode(Document $collection, Document $document, array $selectio
61056108 }
61066109 }
61076110
6108- $ attributes = array_merge ($ attributes , $ this ->getInternalAttributes ());
6111+ $ attributes = \ array_merge ($ attributes , $ this ->getInternalAttributes ());
61096112
61106113 foreach ($ attributes as $ attribute ) {
61116114 $ key = $ attribute ['$id ' ] ?? '' ;
@@ -6125,7 +6128,7 @@ public function decode(Document $collection, Document $document, array $selectio
61256128 $ value = (is_null ($ value )) ? [] : $ value ;
61266129
61276130 foreach ($ value as &$ node ) {
6128- foreach (array_reverse ($ filters ) as $ filter ) {
6131+ foreach (\ array_reverse ($ filters ) as $ filter ) {
61296132 $ node = $ this ->decodeAttribute ($ filter , $ node , $ document );
61306133 }
61316134 }
0 commit comments