From bcf5b4bfab53beb7dafaf6819879f7d4413ce765 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 31 Mar 2023 18:38:37 +1300 Subject: [PATCH] Update related two way attribute --- app/controllers/api/databases.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 066c5bbf12..00ceb3aa24 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -228,7 +228,7 @@ function updateAttribute( throw new Exception(Exception::COLLECTION_NOT_FOUND); } - $attribute = $dbForProject->getDocument('attributes', ID::custom($db->getInternalId() . '_' . $collection->getInternalId() . '_' . $key)); + $attribute = $dbForProject->getDocument('attributes', $db->getInternalId() . '_' . $collection->getInternalId() . '_' . $key); if ($attribute->isEmpty()) { throw new Exception(Exception::ATTRIBUTE_NOT_FOUND); @@ -319,15 +319,24 @@ function updateAttribute( } if ($type === Database::VAR_RELATIONSHIP) { - $options = array_merge($attribute->getAttribute('options', []), $options); + $options = \array_merge($attribute->getAttribute('options', []), $options); $attribute->setAttribute('options', $options); $dbForProject->updateRelationship( collection: $collectionId, key: $key, - twoWay: $options['twoWay'], onDelete: $options['onDelete'], ); + + if ($options['twoWay']) { + $relatedCollection = $dbForProject->getDocument('database_' . $db->getInternalId(), $options['relatedCollection']); + $relatedAttribute = $dbForProject->getDocument('attributes', $db->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $options['twoWayKey']); + $relatedOptions = \array_merge($relatedAttribute->getAttribute('options'), $options); + $relatedAttribute->setAttribute('options', $relatedOptions); + + $dbForProject->updateDocument('attributes', $db->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $options['twoWayKey'], $relatedAttribute); + $dbForProject->deleteCachedDocument('database_' . $db->getInternalId(), $relatedCollection->getId()); + } } else { $dbForProject->updateAttribute( collection: $collectionId,