@@ -1014,7 +1014,7 @@ public function createDocuments(string $collection, array $documents): array
10141014 $ permissions = [];
10151015 $ documentIds = [];
10161016
1017- foreach ($ documents as $ document ) {
1017+ foreach ($ documents as $ index => $ document ) {
10181018 $ attributes = $ document ->getAttributes ();
10191019 $ attributes ['_uid ' ] = $ document ->getId ();
10201020 $ attributes ['_createdAt ' ] = $ document ->getCreatedAt ();
@@ -1049,25 +1049,20 @@ public function createDocuments(string $collection, array $documents): array
10491049 $ batchKeys [] = '( ' . \implode (', ' , $ bindKeys ) . ') ' ;
10501050 foreach (Database::PERMISSIONS as $ type ) {
10511051 foreach ($ document ->getPermissionsByType ($ type ) as $ permission ) {
1052+ $ tenantBind = $ this ->sharedTables ? ", :_tenant_ {$ index }" : '' ;
10521053 $ permission = \str_replace ('" ' , '' , $ permission );
1053- $ permission = "(' {$ type }', ' {$ permission }', ' {$ document ->getId ()}' " ;
1054-
1055- if ($ this ->sharedTables ) {
1056- $ permission .= ", :_tenant) " ;
1057- } else {
1058- $ permission .= ") " ;
1059- }
1060-
1054+ $ permission = "(' {$ type }', ' {$ permission }', :_uid_ {$ index } {$ tenantBind }) " ;
10611055 $ permissions [] = $ permission ;
10621056 }
10631057 }
10641058 }
10651059
1066- $ stmt = $ this ->getPDO ()->prepare (
1067- "
1060+ $ batchKeys = \implode (', ' , $ batchKeys );
1061+
1062+ $ stmt = $ this ->getPDO ()->prepare ("
10681063 INSERT INTO {$ this ->getSQLTable ($ name )} {$ columns }
1069- VALUES " . \implode ( ' , ' , $ batchKeys)
1070- );
1064+ VALUES { $ batchKeys}
1065+ " );
10711066
10721067 foreach ($ bindValues as $ key => $ value ) {
10731068 $ stmt ->bindValue ($ key , $ value , $ this ->getPDOType ($ value ));
@@ -1076,22 +1071,21 @@ public function createDocuments(string $collection, array $documents): array
10761071 $ stmt ->execute ();
10771072
10781073 if (!empty ($ permissions )) {
1074+ $ tenantColumn = $ this ->sharedTables ? ', _tenant ' : '' ;
1075+ $ permissions = \implode (', ' , $ permissions );
1076+
10791077 $ sqlPermissions = "
1080- INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (_type, _permission, _document
1078+ INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (_type, _permission, _document {$ tenantColumn })
1079+ VALUES {$ permissions };
10811080 " ;
10821081
1083- if ($ this ->sharedTables ) {
1084- $ sqlPermissions .= ', _tenant) ' ;
1085- } else {
1086- $ sqlPermissions .= ") " ;
1087- }
1088-
1089- $ sqlPermissions .= " VALUES " . \implode (', ' , $ permissions );
1090-
10911082 $ stmtPermissions = $ this ->getPDO ()->prepare ($ sqlPermissions );
10921083
1093- if ($ this ->sharedTables ) {
1094- $ stmtPermissions ->bindValue (':_tenant ' , $ this ->tenant );
1084+ foreach ($ documents as $ index => $ document ) {
1085+ $ stmtPermissions ->bindValue (":_uid_ {$ index }" , $ document ->getId ());
1086+ if ($ this ->sharedTables ) {
1087+ $ stmtPermissions ->bindValue (":_tenant_ {$ index }" , $ document ->getTenant ());
1088+ }
10951089 }
10961090
10971091 $ stmtPermissions ?->execute();
0 commit comments