@@ -1358,6 +1358,7 @@ public function createOrUpdateDocuments(
13581358 $ documentTenants = [];
13591359
13601360 foreach ($ documents as $ document ) {
1361+ $ document = $ document ['new ' ];
13611362 $ attributes = $ document ->getAttributes ();
13621363 $ attributes ['_uid ' ] = $ document ->getId ();
13631364 $ attributes ['_createdAt ' ] = $ document ->getCreatedAt ();
@@ -1441,54 +1442,25 @@ public function createOrUpdateDocuments(
14411442 }
14421443
14431444 $ stmt ->execute ();
1444-
1445- // Fetch existing permissions in bulk after data updates
1446- $ sql = "
1447- SELECT _document, _type, _permission
1448- FROM {$ this ->getSQLTable ($ name . '_perms ' )}
1449- WHERE _document IN ( " . \implode (', ' , \array_map (fn ($ index ) => ":_key_ {$ index }" , \array_keys ($ documents ))) . ")
1450- {$ this ->getTenantQuery ($ collection , tenantCount: \count ($ documentTenants ))}
1451- " ;
1452-
1453- $ stmt = $ this ->getPDO ()->prepare ($ sql );
1454-
1455- foreach ($ documents as $ index => $ document ) {
1456- $ stmt ->bindValue (":_key_ {$ index }" , $ document ->getId ());
1457- }
1458-
1459- if ($ this ->sharedTables ) {
1460- foreach ($ documentTenants as $ index => $ tenant ) {
1461- $ stmt ->bindValue (":_tenant_ {$ index }" , $ tenant );
1462- }
1463- }
1464-
1465- $ stmt ->execute ();
1466- $ existing = $ stmt ->fetchAll ();
14671445 $ stmt ->closeCursor ();
14681446
1469- // Group permissions by document
1470- $ permissionsByDocument = [];
1471- foreach ($ existing as $ row ) {
1472- $ permissionsByDocument [$ row ['_document ' ]][$ row ['_type ' ]][] = $ row ['_permission ' ];
1473- }
1474-
1475- foreach ($ documentIds as $ id ) {
1476- foreach (Database::PERMISSIONS as $ type ) {
1477- $ permissionsByDocument [$ id ][$ type ] = $ permissionsByDocument [$ id ][$ type ] ?? [];
1478- }
1479- }
1480-
14811447 $ removeQueries = [];
14821448 $ removeBindValues = [];
14831449 $ addQueries = [];
14841450 $ addBindValues = [];
14851451
14861452 foreach ($ documents as $ index => $ document ) {
1487- $ currentPermissions = $ permissionsByDocument [$ document ->getId ()] ?? [];
1453+ $ old = $ document ['old ' ];
1454+ $ document = $ document ['new ' ];
1455+
1456+ $ current = [];
1457+ foreach (Database::PERMISSIONS as $ type ) {
1458+ $ current [$ type ] = $ old ->getPermissionsByType ($ type ) ?? [];
1459+ }
14881460
14891461 // Calculate removals
14901462 foreach (Database::PERMISSIONS as $ type ) {
1491- $ toRemove = \array_diff ($ currentPermissions [$ type ] ?? [], $ document ->getPermissionsByType ($ type ));
1463+ $ toRemove = \array_diff ($ current [$ type ] ?? [], $ document ->getPermissionsByType ($ type ));
14921464 if (!empty ($ toRemove )) {
14931465 $ removeQueries [] = "(
14941466 _document = :_uid_ {$ index }
@@ -1506,7 +1478,8 @@ public function createOrUpdateDocuments(
15061478
15071479 // Calculate additions
15081480 foreach (Database::PERMISSIONS as $ type ) {
1509- $ toAdd = \array_diff ($ document ->getPermissionsByType ($ type ), $ currentPermissions [$ type ] ?? []);
1481+ $ toAdd = \array_diff ($ document ->getPermissionsByType ($ type ), $ current [$ type ] ?? []);
1482+
15101483 foreach ($ toAdd as $ i => $ permission ) {
15111484 $ addQuery = "(:_uid_ {$ index }, ' {$ type }', :add_ {$ type }_ {$ index }_ {$ i }" ;
15121485
@@ -1552,8 +1525,8 @@ public function createOrUpdateDocuments(
15521525
15531526 $ internalIds = $ this ->getInternalIds ($ collection , $ documentIds , $ documentTenants );
15541527 foreach ($ documents as $ document ) {
1555- if (isset ($ internalIds [$ document ->getId ()])) {
1556- $ document ['$internalId ' ] = $ internalIds [$ document ->getId ()];
1528+ if (isset ($ internalIds [$ document[ ' new ' ] ->getId ()])) {
1529+ $ document ['new ' ][ ' $internalId ' ] = $ internalIds [$ document[ ' new ' ] ->getId ()];
15571530 }
15581531 }
15591532 } catch (PDOException $ e ) {
0 commit comments