From 161eb0b66fab19917f286635fcd9b0acbfd91a37 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 29 Mar 2023 19:26:58 +1300 Subject: [PATCH] Create two-way attribute on related collection --- app/controllers/api/databases.php | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 39fceb633a..6c859e9f5c 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -139,6 +139,46 @@ function createAttribute(string $databaseId, string $collectionId, Document $att $dbForProject->deleteCachedDocument('database_' . $db->getInternalId(), $collectionId); $dbForProject->deleteCachedCollection('database_' . $db->getInternalId() . '_collection_' . $collection->getInternalId()); + if ($type === Database::VAR_RELATIONSHIP) { + if ($options['twoWay']) { + $twoWayKey = $options['twoWayKey']; + $options['relatedCollection'] = $collection->getId(); + $options['twoWayKey'] = $key; + + try { + $twoWayAttribute = new Document([ + '$id' => ID::custom($db->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $twoWayKey), + 'key' => $twoWayKey, + 'databaseInternalId' => $db->getInternalId(), + 'databaseId' => $db->getId(), + 'collectionInternalId' => $relatedCollection->getInternalId(), + 'collectionId' => $relatedCollection->getId(), + 'type' => $type, + 'status' => 'available', // processing, available, failed, deleting, stuck + 'size' => $size, + 'required' => $required, + 'signed' => $signed, + 'default' => $default, + 'array' => $array, + 'format' => $format, + 'formatOptions' => $formatOptions, + 'filters' => $filters, + 'options' => $options, + ]); + + $dbForProject->checkAttribute($relatedCollection, $twoWayAttribute); + $twoWayAttribute = $dbForProject->createDocument('attributes', $twoWayAttribute); + } catch (DuplicateException) { + throw new Exception(Exception::ATTRIBUTE_ALREADY_EXISTS); + } catch (LimitException) { + throw new Exception(Exception::ATTRIBUTE_LIMIT_EXCEEDED, 'Attribute limit exceeded'); + } + + $dbForProject->deleteCachedDocument('database_' . $db->getInternalId(), $relatedCollection->getId()); + $dbForProject->deleteCachedCollection('database_' . $db->getInternalId() . '_collection_' . $relatedCollection->getInternalId()); + } + } + $database ->setType(DATABASE_TYPE_CREATE_ATTRIBUTE) ->setDatabase($db)