@@ -382,10 +382,9 @@ public function createAttribute(string $collection, string $id, string $type, in
382382 * @param int $size
383383 * @param bool $signed
384384 * @param bool $array
385- * @param string $newKey
385+ * @param string|null $newKey
386386 * @return bool
387- * @throws Exception
388- * @throws PDOException
387+ * @throws DatabaseException
389388 */
390389 public function updateAttribute (string $ collection , string $ id , string $ type , int $ size , bool $ signed = true , bool $ array = false , ?string $ newKey = null ): bool
391390 {
@@ -853,7 +852,7 @@ public function createDocument(string $collection, Document $document): Document
853852 $ attributes ['_permissions ' ] = \json_encode ($ document ->getPermissions ());
854853
855854 if ($ this ->sharedTables ) {
856- $ attributes ['_tenant ' ] = $ this -> tenant ;
855+ $ attributes ['_tenant ' ] = $ document -> getTenant () ;
857856 }
858857
859858 $ name = $ this ->filter ($ collection );
@@ -896,58 +895,46 @@ public function createDocument(string $collection, Document $document): Document
896895
897896 $ attributeIndex = 0 ;
898897 foreach ($ attributes as $ value ) {
899- if (is_array ($ value )) {
900- $ value = json_encode ($ value );
898+ if (\ is_array ($ value )) {
899+ $ value = \ json_encode ($ value );
901900 }
902901
903902 $ bindKey = 'key_ ' . $ attributeIndex ;
904903 $ attribute = $ this ->filter ($ attribute );
905- $ value = (is_bool ($ value )) ? (int )$ value : $ value ;
904+ $ value = (\ is_bool ($ value )) ? (int )$ value : $ value ;
906905 $ stmt ->bindValue (': ' . $ bindKey , $ value , $ this ->getPDOType ($ value ));
907906 $ attributeIndex ++;
908907 }
909908
910909 $ permissions = [];
911910 foreach (Database::PERMISSIONS as $ type ) {
912911 foreach ($ document ->getPermissionsByType ($ type ) as $ permission ) {
912+ $ tenantBind = $ this ->sharedTables ? ", :_tenant " : '' ;
913913 $ permission = \str_replace ('" ' , '' , $ permission );
914- $ permission = "(' {$ type }', ' {$ permission }', ' {$ document ->getId ()}' " ;
915-
916- if ($ this ->sharedTables ) {
917- $ permission .= ", :_tenant) " ;
918- } else {
919- $ permission .= ") " ;
920- }
921-
914+ $ permission = "(' {$ type }', ' {$ permission }', :_uid {$ tenantBind }) " ;
922915 $ permissions [] = $ permission ;
923916 }
924917 }
925918
926919 if (!empty ($ permissions )) {
920+ $ tenantColumn = $ this ->sharedTables ? ', _tenant ' : '' ;
927921 $ permissions = \implode (', ' , $ permissions );
928922
929923 $ sqlPermissions = "
930- INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (_type, _permission, _document
931- " ;
932-
933- if ($ this ->sharedTables ) {
934- $ sqlPermissions .= ', _tenant) ' ;
935- } else {
936- $ sqlPermissions .= ") " ;
937- }
924+ INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (_type, _permission, _document {$ tenantColumn })
925+ VALUES {$ permissions };
926+ " ;
938927
939- $ sqlPermissions .= " VALUES {$ permissions }" ;
940- $ sqlPermissions = $ this ->trigger (Database::EVENT_PERMISSIONS_CREATE , $ sqlPermissions );
941928 $ stmtPermissions = $ this ->getPDO ()->prepare ($ sqlPermissions );
942-
929+ $ stmtPermissions -> bindValue ( ' :_uid ' , $ document -> getId ());
943930 if ($ this ->sharedTables ) {
944- $ stmtPermissions ->bindValue (':_tenant ' , $ this -> tenant );
931+ $ stmtPermissions ->bindValue (':_tenant ' , $ document -> getTenant () );
945932 }
946933 }
947934
948935 $ stmt ->execute ();
949936
950- $ document ['$internalId ' ] = $ this ->getDocument ( $ collection , $ document -> getId (), [Query:: select ([ ' $internalId ' ])])-> getInternalId ();
937+ $ document ['$internalId ' ] = $ this ->pdo -> lastInsertId ();
951938
952939 if (empty ($ document ['$internalId ' ])) {
953940 throw new DatabaseException ('Error creating document empty "$internalId" ' );
@@ -1014,10 +1001,7 @@ public function createDocuments(string $collection, array $documents): array
10141001 $ permissions = [];
10151002 $ documentIds = [];
10161003
1017- foreach ($ documents as $ document ) {
1018- /**
1019- * @var Document $document
1020- */
1004+ foreach ($ documents as $ index => $ document ) {
10211005 $ attributes = $ document ->getAttributes ();
10221006 $ attributes ['_uid ' ] = $ document ->getId ();
10231007 $ attributes ['_createdAt ' ] = $ document ->getCreatedAt ();
@@ -1032,7 +1016,7 @@ public function createDocuments(string $collection, array $documents): array
10321016 }
10331017
10341018 if ($ this ->sharedTables ) {
1035- $ attributes ['_tenant ' ] = $ this -> tenant ;
1019+ $ attributes ['_tenant ' ] = $ document -> getTenant () ;
10361020 }
10371021
10381022 $ bindKeys = [];
@@ -1052,25 +1036,20 @@ public function createDocuments(string $collection, array $documents): array
10521036 $ batchKeys [] = '( ' . \implode (', ' , $ bindKeys ) . ') ' ;
10531037 foreach (Database::PERMISSIONS as $ type ) {
10541038 foreach ($ document ->getPermissionsByType ($ type ) as $ permission ) {
1039+ $ tenantBind = $ this ->sharedTables ? ", :_tenant_ {$ index }" : '' ;
10551040 $ permission = \str_replace ('" ' , '' , $ permission );
1056- $ permission = "(' {$ type }', ' {$ permission }', ' {$ document ->getId ()}' " ;
1057-
1058- if ($ this ->sharedTables ) {
1059- $ permission .= ", :_tenant) " ;
1060- } else {
1061- $ permission .= ") " ;
1062- }
1063-
1041+ $ permission = "(' {$ type }', ' {$ permission }', :_uid_ {$ index } {$ tenantBind }) " ;
10641042 $ permissions [] = $ permission ;
10651043 }
10661044 }
10671045 }
10681046
1069- $ stmt = $ this ->getPDO ()->prepare (
1070- "
1047+ $ batchKeys = \implode (', ' , $ batchKeys );
1048+
1049+ $ stmt = $ this ->getPDO ()->prepare ("
10711050 INSERT INTO {$ this ->getSQLTable ($ name )} {$ columns }
1072- VALUES " . \implode ( ' , ' , $ batchKeys)
1073- );
1051+ VALUES { $ batchKeys}
1052+ " );
10741053
10751054 foreach ($ bindValues as $ key => $ value ) {
10761055 $ stmt ->bindValue ($ key , $ value , $ this ->getPDOType ($ value ));
@@ -1079,22 +1058,21 @@ public function createDocuments(string $collection, array $documents): array
10791058 $ stmt ->execute ();
10801059
10811060 if (!empty ($ permissions )) {
1061+ $ tenantColumn = $ this ->sharedTables ? ', _tenant ' : '' ;
1062+ $ permissions = \implode (', ' , $ permissions );
1063+
10821064 $ sqlPermissions = "
1083- INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (_type, _permission, _document
1065+ INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (_type, _permission, _document {$ tenantColumn })
1066+ VALUES {$ permissions };
10841067 " ;
10851068
1086- if ($ this ->sharedTables ) {
1087- $ sqlPermissions .= ', _tenant) ' ;
1088- } else {
1089- $ sqlPermissions .= ") " ;
1090- }
1091-
1092- $ sqlPermissions .= " VALUES " . \implode (', ' , $ permissions );
1093-
10941069 $ stmtPermissions = $ this ->getPDO ()->prepare ($ sqlPermissions );
10951070
1096- if ($ this ->sharedTables ) {
1097- $ stmtPermissions ->bindValue (':_tenant ' , $ this ->tenant );
1071+ foreach ($ documents as $ index => $ document ) {
1072+ $ stmtPermissions ->bindValue (":_uid_ {$ index }" , $ document ->getId ());
1073+ if ($ this ->sharedTables ) {
1074+ $ stmtPermissions ->bindValue (":_tenant_ {$ index }" , $ document ->getTenant ());
1075+ }
10981076 }
10991077
11001078 $ stmtPermissions ?->execute();
@@ -2228,7 +2206,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
22282206 unset($ results [$ index ]['_id ' ]);
22292207 }
22302208 if (\array_key_exists ('_tenant ' , $ document )) {
2231- $ results [ $ index ][ ' $tenant ' ] = $ document ['_tenant ' ];
2209+ $ document [ ' $tenant ' ] = $ document [ ' _tenant ' ] === null ? null : ( int ) $ document ['_tenant ' ];
22322210 unset($ results [$ index ]['_tenant ' ]);
22332211 }
22342212 if (\array_key_exists ('_createdAt ' , $ document )) {
0 commit comments