diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 31a4cfd53d..ee5e69bc76 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -1569,13 +1569,10 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/relati ); $options = $attribute->getAttribute('options', []); - $attribute->setAttribute('relatedCollection', $options['relatedCollection'] ?? null); - $attribute->setAttribute('relationType', $options['relationType'] ?? null); - $attribute->setAttribute('twoWay', $options['twoWay'] ?? null); - $attribute->setAttribute('twoWayKey', $options['twoWayKey'] ?? null); - $attribute->setAttribute('onUpdate', $options['onUpdate'] ?? null); - $attribute->setAttribute('onDelete', $options['onDelete'] ?? null); - $attribute->setAttribute('side', $options['side'] ?? null); + + foreach ($options as $key => $option) { + $attribute->setAttribute($key, $option); + } $response ->setStatusCode(Response::STATUS_CODE_ACCEPTED) @@ -1673,6 +1670,11 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/attributes/:key') // Select response model based on type and format $type = $attribute->getAttribute('type'); $format = $attribute->getAttribute('format'); + $options = $attribute->getAttribute('options', []); + + foreach ($options as $key => $option) { + $attribute->setAttribute($key, $option); + } $model = match ($type) { Database::VAR_RELATIONSHIP => Response::MODEL_ATTRIBUTE_RELATIONSHIP, diff --git a/src/Appwrite/Utopia/Response/Model/AttributeRelationship.php b/src/Appwrite/Utopia/Response/Model/AttributeRelationship.php index 22d088b874..2045234dfd 100644 --- a/src/Appwrite/Utopia/Response/Model/AttributeRelationship.php +++ b/src/Appwrite/Utopia/Response/Model/AttributeRelationship.php @@ -27,22 +27,43 @@ class AttributeRelationship extends Attribute 'type' => self::TYPE_STRING, 'description' => 'Default value for attribute when not provided. Only null is optional', 'default' => null, + 'example' => '', + ]) + ->addRule('relatedCollection', [ + 'type' => self::TYPE_STRING, + 'description' => 'The Id of the related collection', + 'default' => null, + 'example' => 'collection', + ]) + ->addRule('relationType', [ + 'type' => self::TYPE_STRING, + 'description' => 'The type of the relationship ', + 'default' => null, + 'example' => 'oneToOne|oneToMany|manyToOne|manyToMany', + ]) + ->addRule('twoWay', [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Is the relationship going two ways?', + 'default' => null, 'example' => 'relationship', ]) - ->addRule('options', [ - 'type' => [ - 'relatedCollection', - 'relationType', - 'twoWay', - 'twoWayKey', - 'onUpdate', - 'onDelete', - 'side', - ], - 'description' => 'Options attributes.', - 'default' => [], - 'example' => new \stdClass(), - 'array' => true, + ->addRule('twoWayKey', [ + 'type' => self::TYPE_STRING, + 'description' => 'The key of the 2 way relationship', + 'default' => null, + 'example' => 'string', + ]) + ->addRule('onUpdate', [ + 'type' => self::TYPE_STRING, + 'description' => 'How to set related documents after parent document is updated', + 'default' => null, + 'example' => 'restrict|cascade|setNull', + ]) + ->addRule('onDelete', [ + 'type' => self::TYPE_STRING, + 'description' => 'How to set related documents after parent document is deleted', + 'default' => null, + 'example' => 'restrict|cascade|setNull', ]) ; } diff --git a/tests/e2e/Services/Databases/DatabasesBase.php b/tests/e2e/Services/Databases/DatabasesBase.php index 6a5c57a305..75e7f375be 100644 --- a/tests/e2e/Services/Databases/DatabasesBase.php +++ b/tests/e2e/Services/Databases/DatabasesBase.php @@ -317,11 +317,11 @@ trait DatabasesBase $this->assertEquals('relationship', $attribute['body']['type']); $this->assertEquals(false, $attribute['body']['required']); $this->assertEquals(false, $attribute['body']['array']); - $this->assertEquals('oneToOne', $attribute['body']['options']['relationType']); - $this->assertEquals(false, $attribute['body']['options']['twoWay']); - $this->assertEquals('cascade', $attribute['body']['options']['onUpdate']); - $this->assertEquals('restrict', $attribute['body']['options']['onDelete']); - $this->assertEquals('libraryId', $attribute['body']['options']['id']); + $this->assertEquals('oneToOne', $attribute['body']['relationType']); + $this->assertEquals(false, $attribute['body']['twoWay']); + $this->assertEquals('cascade', $attribute['body']['onUpdate']); + $this->assertEquals('restrict', $attribute['body']['onDelete']); + //$this->assertEquals('libraryId', $attribute['body']['id']); // $person1 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $person['body']['$id'] . '/documents', array_merge([ 'content-type' => 'application/json', @@ -356,7 +356,6 @@ trait DatabasesBase $this->assertEquals(1, $documents['body']['total']); $this->assertEquals('Library 1', $documents['body']['documents'][0]['libraryId']['libraryName']); - // $documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $person['body']['$id'] . '/documents', array_merge([ // 'content-type' => 'application/json', // 'x-appwrite-project' => $this->getProject()['$id'], @@ -371,13 +370,10 @@ trait DatabasesBase 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ])); - var_dump($response); $this->assertEquals(204, $response['headers']['status-code']); die; - - return []; }