@@ -1051,6 +1051,7 @@ public function createDocument(string $collection, Document $document): Document
10511051 * @return array<Document>
10521052 *
10531053 * @throws DuplicateException
1054+ * @throws \Throwable
10541055 */
10551056 public function createDocuments (string $ collection , array $ documents ): array
10561057 {
@@ -1060,37 +1061,44 @@ public function createDocuments(string $collection, array $documents): array
10601061
10611062 try {
10621063 $ name = $ this ->filter ($ collection );
1064+
10631065 $ attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS ;
10641066
10651067 $ hasSequence = null ;
10661068 foreach ($ documents as $ document ) {
10671069 $ attributes = $ document ->getAttributes ();
1068- $ attributeKeys = array_merge ( $ attributeKeys , array_keys ($ attributes )) ;
1070+ $ attributeKeys = [... $ attributeKeys , ... \ array_keys ($ attributes )] ;
10691071
10701072 if ($ hasSequence === null ) {
10711073 $ hasSequence = !empty ($ document ->getSequence ());
10721074 } elseif ($ hasSequence == empty ($ document ->getSequence ())) {
10731075 throw new DatabaseException ('All documents must have an sequence if one is set ' );
10741076 }
10751077 }
1078+
10761079 $ attributeKeys = array_unique ($ attributeKeys );
10771080
1081+ if ($ hasSequence ) {
1082+ $ attributeKeys [] = '_id ' ;
1083+ }
1084+
10781085 if ($ this ->sharedTables ) {
10791086 $ attributeKeys [] = '_tenant ' ;
10801087 }
10811088
10821089 $ columns = [];
10831090 foreach ($ attributeKeys as $ key => $ attribute ) {
1084- $ columns [$ key ] = "\"{ $ this ->filter ($ attribute )}\"" ;
1091+ $ columns [$ key ] = $ this ->quote ( $ this -> filter ($ attribute )) ;
10851092 }
1086- $ columns = '( ' . \implode (', ' , $ columns ) . ') ' ;
10871093
1088- $ sequences = [] ;
1094+ $ columns = ' ( ' . \implode ( ' , ' , $ columns ) . ' ) ' ;
10891095
10901096 $ bindIndex = 0 ;
10911097 $ batchKeys = [];
10921098 $ bindValues = [];
10931099 $ permissions = [];
1100+ $ documentIds = [];
1101+ $ documentTenants = [];
10941102
10951103 foreach ($ documents as $ index => $ document ) {
10961104 $ attributes = $ document ->getAttributes ();
@@ -1100,13 +1108,14 @@ public function createDocuments(string $collection, array $documents): array
11001108 $ attributes ['_permissions ' ] = \json_encode ($ document ->getPermissions ());
11011109
11021110 if (!empty ($ document ->getSequence ())) {
1103- $ sequences [$ document ->getId ()] = true ;
11041111 $ attributes ['_id ' ] = $ document ->getSequence ();
1105- $ attributeKeys [] = '_id ' ;
1112+ } else {
1113+ $ documentIds [] = $ document ->getId ();
11061114 }
11071115
11081116 if ($ this ->sharedTables ) {
11091117 $ attributes ['_tenant ' ] = $ document ->getTenant ();
1118+ $ documentTenants [] = $ document ->getTenant ();
11101119 }
11111120
11121121 $ bindKeys = [];
@@ -1124,6 +1133,7 @@ public function createDocuments(string $collection, array $documents): array
11241133 }
11251134
11261135 $ batchKeys [] = '( ' . \implode (', ' , $ bindKeys ) . ') ' ;
1136+
11271137 foreach (Database::PERMISSIONS as $ type ) {
11281138 foreach ($ document ->getPermissionsByType ($ type ) as $ permission ) {
11291139 $ tenantBind = $ this ->sharedTables ? ", :_tenant_ {$ index }" : '' ;
@@ -1167,18 +1177,20 @@ public function createDocuments(string $collection, array $documents): array
11671177
11681178 $ this ->execute ($ stmtPermissions );
11691179 }
1170- } catch (PDOException $ e ) {
1171- throw $ this ->processException ($ e );
1172- }
11731180
1174- foreach ($ documents as $ document ) {
1175- if (!isset ($ sequences [$ document ->getId ()])) {
1176- $ document ['$sequence ' ] = $ this ->getDocument (
1177- $ collection ,
1178- $ document ->getId (),
1179- [Query::select (['$sequence ' ])]
1180- )->getSequence ();
1181+ $ sequences = $ this ->getSequences (
1182+ $ collection ,
1183+ $ documentIds ,
1184+ $ documentTenants
1185+ );
1186+
1187+ foreach ($ documents as $ document ) {
1188+ if (isset ($ sequences [$ document ->getId ()])) {
1189+ $ document ['$sequence ' ] = $ sequences [$ document ->getId ()];
1190+ }
11811191 }
1192+ } catch (PDOException $ e ) {
1193+ throw $ this ->processException ($ e );
11821194 }
11831195
11841196 return $ documents ;
0 commit comments