@@ -332,9 +332,13 @@ public function deleteCollection(string $id): bool
332332
333333 $ sql = $ this ->trigger (Database::EVENT_COLLECTION_DELETE , $ sql );
334334
335- return $ this ->getPDO ()
336- ->prepare ($ sql )
337- ->execute ();
335+ try {
336+ return $ this ->getPDO ()
337+ ->prepare ($ sql )
338+ ->execute ();
339+ } catch (PDOException $ e ) {
340+ throw $ this ->processException ($ e );
341+ }
338342 }
339343
340344 /**
@@ -763,26 +767,31 @@ public function createIndex(string $collection, string $id, string $type, array
763767 throw new NotFoundException ('Collection not found ' );
764768 }
765769
770+ /**
771+ * We do not have internalId's added to list, since we check only for array field
772+ */
766773 $ collectionAttributes = \json_decode ($ collection ->getAttribute ('attributes ' , []), true );
767774
768775 $ id = $ this ->filter ($ id );
769776
770777 foreach ($ attributes as $ i => $ attr ) {
771- $ collectionAttribute = \array_filter ($ collectionAttributes , fn ($ collectionAttribute ) => array_key_exists ('key ' , $ collectionAttribute ) && $ collectionAttribute ['key ' ] === $ attr );
772- $ collectionAttribute = end ($ collectionAttribute );
778+ $ attribute = null ;
779+ foreach ($ collectionAttributes as $ collectionAttribute ) {
780+ if (\strtolower ($ collectionAttribute ['$id ' ]) === \strtolower ($ attr )) {
781+ $ attribute = $ collectionAttribute ;
782+ break ;
783+ }
784+ }
785+
773786 $ order = empty ($ orders [$ i ]) || Database::INDEX_FULLTEXT === $ type ? '' : $ orders [$ i ];
774787 $ length = empty ($ lengths [$ i ]) ? '' : '( ' . (int )$ lengths [$ i ] . ') ' ;
775788
776- $ attr = match ($ attr ) {
777- '$id ' => '_uid ' ,
778- '$createdAt ' => '_createdAt ' ,
779- '$updatedAt ' => '_updatedAt ' ,
780- default => $ this ->filter ($ attr ),
781- };
789+ $ attr = $ this ->getInternalKeyForAttribute ($ attr );
790+ $ attr = $ this ->filter ($ attr );
782791
783792 $ attributes [$ i ] = "` {$ attr }` {$ length } {$ order }" ;
784793
785- if (!empty ($ collectionAttribute ['array ' ]) && $ this -> getSupportForCastIndexArray ( )) {
794+ if ($ this -> getSupportForCastIndexArray () && !empty ($ attribute ['array ' ])) {
786795 $ attributes [$ i ] = '(CAST(` ' . $ attr . '` AS char( ' . Database::ARRAY_INDEX_LENGTH . ') ARRAY)) ' ;
787796 }
788797 }
@@ -1130,10 +1139,6 @@ public function updateDocument(string $collection, string $id, Document $documen
11301139 $ attributes ['_updatedAt ' ] = $ document ->getUpdatedAt ();
11311140 $ attributes ['_permissions ' ] = json_encode ($ document ->getPermissions ());
11321141
1133- if ($ this ->sharedTables ) {
1134- $ attributes ['_tenant ' ] = $ this ->tenant ;
1135- }
1136-
11371142 $ name = $ this ->filter ($ collection );
11381143 $ columns = '' ;
11391144
@@ -1298,15 +1303,15 @@ public function updateDocument(string $collection, string $id, Document $documen
12981303 $ sql = "
12991304 UPDATE {$ this ->getSQLTable ($ name )}
13001305 SET {$ columns } _uid = :_newUid
1301- WHERE _uid = :_existingUid
1306+ WHERE _id=:_internalId
13021307 {$ this ->getTenantQuery ($ collection )}
13031308 " ;
13041309
13051310 $ sql = $ this ->trigger (Database::EVENT_DOCUMENT_UPDATE , $ sql );
13061311
13071312 $ stmt = $ this ->getPDO ()->prepare ($ sql );
13081313
1309- $ stmt ->bindValue (':_existingUid ' , $ id );
1314+ $ stmt ->bindValue (':_internalId ' , $ document -> getInternalId () );
13101315 $ stmt ->bindValue (':_newUid ' , $ document ->getId ());
13111316
13121317 if ($ this ->sharedTables ) {
@@ -2332,6 +2337,13 @@ protected function processException(PDOException $e): \Exception
23322337 return new NotFoundException ('Collection not found ' , $ e ->getCode (), $ e );
23332338 }
23342339
2340+ // Unknown collection
2341+ // We have two of same, because docs point to 1051.
2342+ // Keeping previous 1049 (above) just in case it's for older versions
2343+ if ($ e ->getCode () === '42S02 ' && isset ($ e ->errorInfo [1 ]) && $ e ->errorInfo [1 ] === 1051 ) {
2344+ return new NotFoundException ('Collection not found ' , $ e ->getCode (), $ e );
2345+ }
2346+
23352347 return $ e ;
23362348 }
23372349
0 commit comments