File tree Expand file tree Collapse file tree 2 files changed +17
-4
lines changed
Expand file tree Collapse file tree 2 files changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -757,7 +757,18 @@ public function createDocuments(string $collection, array $documents): array
757757 $ name = $ this ->getNamespace () . '_ ' . $ this ->filter ($ collection );
758758
759759 $ records = [];
760+ $ hasInternalId = null ;
761+ $ documents = array_map (fn ($ doc ) => clone $ doc , $ documents );
762+
760763 foreach ($ documents as $ document ) {
764+ $ internalId = $ document ->getInternalId ();
765+
766+ if ($ hasInternalId === null ) {
767+ $ hasInternalId = !empty ($ internalId );
768+ } elseif ($ hasInternalId == empty ($ internalId )) {
769+ throw new DatabaseException ('All documents must have an internalId if one is set ' );
770+ }
771+
761772 $ document ->removeAttribute ('$internalId ' );
762773
763774 if ($ this ->sharedTables ) {
@@ -767,6 +778,10 @@ public function createDocuments(string $collection, array $documents): array
767778 $ record = $ this ->replaceChars ('$ ' , '_ ' , (array )$ document );
768779 $ record = $ this ->timeToMongo ($ record );
769780
781+ if (!empty ($ internalId )) {
782+ $ record ['_id ' ] = $ internalId ;
783+ }
784+
770785 $ records [] = $ this ->removeNullKeys ($ record );
771786 }
772787
Original file line number Diff line number Diff line change @@ -2291,16 +2291,14 @@ public function testCreateDocumentsWithDifferentAttributes(): void
22912291 try {
22922292 static::getDatabase()->createDocuments($collection, $documents);
22932293 $this->fail('Failed to throw exception');
2294- } catch (Throwable $e) {
2295- $this->assertTrue($e instanceof DatabaseException);
2294+ } catch (DatabaseException $e) {
22962295 }
22972296
22982297 $documents = array_reverse($documents);
22992298 try {
23002299 static::getDatabase()->createDocuments($collection, $documents);
23012300 $this->fail('Failed to throw exception');
2302- } catch (Throwable $e) {
2303- $this->assertTrue($e instanceof DatabaseException);
2301+ } catch (DatabaseException $e) {
23042302 }
23052303
23062304 static::getDatabase()->deleteCollection($collection);
You can’t perform that action at this time.
0 commit comments