1
0
Fork 0
mirror of synced 2024-10-01 01:37:56 +13:00

Model relationshipOptions

This commit is contained in:
fogelito 2023-03-14 15:18:30 +02:00
parent 6306ec59ba
commit 40152fbd6c
2 changed files with 25 additions and 13 deletions

View file

@ -68,7 +68,7 @@ function createAttribute(string $databaseId, string $collectionId, Document $att
$formatOptions = $attribute->getAttribute('formatOptions', []);
$filters = $attribute->getAttribute('filters', []); // filters are hidden from the endpoint
$default = $attribute->getAttribute('default');
$options = $attribute->getAttribute('options', []);
$relationshipOptions = $attribute->getAttribute('relationshipOptions', []);
$db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
@ -98,7 +98,7 @@ function createAttribute(string $databaseId, string $collectionId, Document $att
}
if ($type === Database::VAR_RELATIONSHIP) {
$relatedCollection = $dbForProject->getDocument('database_' . $db->getInternalId(), $options['relatedCollectionId']);
$relatedCollection = $dbForProject->getDocument('database_' . $db->getInternalId(), $relationshipOptions['relatedCollection']);
if ($relatedCollection->isEmpty()) {
throw new Exception(Exception::COLLECTION_NOT_FOUND);
}
@ -122,7 +122,7 @@ function createAttribute(string $databaseId, string $collectionId, Document $att
'format' => $format,
'formatOptions' => $formatOptions,
'filters' => $filters,
'options' => $options, // Do we want all options?
'relationshipOptions' => $relationshipOptions,
]);
$dbForProject->checkAttribute($collection, $attribute);
@ -1394,8 +1394,8 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/relati
'default' => null,
'array' => false,
'filters' => [],
'options' => [
'relatedCollectionId' => $relatedCollectionId,
'relationshipOptions' => [
'relatedCollection' => $relatedCollectionId,
'relationType' => $type,
'twoWay' => $twoWay,
'twoWayKey' => $twoWayKey,
@ -1409,6 +1409,18 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/relati
$events
);
$options = $attribute->getAttribute('relationshipOptions', []);
if (!empty($options)) {
$attribute->setAttribute('relatedCollection', $options['relatedCollection']);
$attribute->setAttribute('relationType', $options['relationType']);
$attribute->setAttribute('twoWay', $options['twoWay']);
$attribute->setAttribute('twoWayKey', $options['twoWayKey']);
$attribute->setAttribute('onUpdate', $options['onUpdate']);
$attribute->setAttribute('onDelete', $options['onDelete']);
$attribute->setAttribute('side', $options['side']);
}
$response
->setStatusCode(Response::STATUS_CODE_ACCEPTED)
->dynamic($attribute, Response::MODEL_ATTRIBUTE_RELATIONSHIP);

View file

@ -91,22 +91,22 @@ class DatabaseV1 extends Worker
$format = $attribute->getAttribute('format', '');
$formatOptions = $attribute->getAttribute('formatOptions', []);
$filters = $attribute->getAttribute('filters', []);
$options = $attribute->getAttribute('options', []);
$relationshipOptions = $attribute->getAttribute('relationshipOptions', []);
$project = $dbForConsole->getDocument('projects', $projectId);
try {
if ($type === Database::VAR_RELATIONSHIP) {
$relatedCollection = $dbForProject->getDocument('database_' . $database->getInternalId(), $options['relatedCollectionId']);
$relatedCollection = $dbForProject->getDocument('database_' . $database->getInternalId(), $relationshipOptions['relatedCollection']);
if (
!$dbForProject->createRelationship(
collection: 'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(),
relatedCollection: 'database_' . $database->getInternalId() . '_collection_' . $relatedCollection->getInternalId(),
type: $options['relationType'],
twoWay: $options['twoWay'],
id: $options['id'],
twoWayKey: $options['twoWayKey'],
onUpdate: $options['onUpdate'],
onDelete: $options['onDelete'],
type: $relationshipOptions['relationType'],
twoWay: $relationshipOptions['twoWay'],
id: $relationshipOptions['id'],
twoWayKey: $relationshipOptions['twoWayKey'],
onUpdate: $relationshipOptions['onUpdate'],
onDelete: $relationshipOptions['onDelete'],
)
) {
throw new Exception('Failed to create Attribute');