1
0
Fork 0
mirror of synced 2024-10-01 17:58:02 +13:00

Create two-way attribute on related collection

This commit is contained in:
Jake Barnby 2023-03-29 19:26:58 +13:00
parent f25bfa805b
commit 161eb0b66f
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C

View file

@ -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)