diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php index 03236471db..2a34c8979b 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php @@ -8,7 +8,6 @@ use Appwrite\Platform\Modules\Databases\Http\Databases\Action as DatabasesAction use Appwrite\Utopia\Database\Validator\CustomId; use Utopia\Database\Database; use Utopia\Database\Document; -use Utopia\Database\Helpers\ID; use Utopia\Database\Validator\Authorization; abstract class Action extends DatabasesAction @@ -252,10 +251,9 @@ abstract class Action extends DatabasesAction } /** - * Validate and normalize a relationship value. - * Returns the relation ID and normalized relation as an array. + * Validate a relationship value and its document ID. */ - protected function validateRelationship(mixed $relation): array + protected function validateRelationship(mixed $relation): void { $relationId = null; @@ -263,10 +261,7 @@ abstract class Action extends DatabasesAction $relationId = $relation->getAttribute('$id'); } elseif (\is_string($relation)) { $relationId = $relation; - } elseif (\is_array($relation) && \array_values($relation) !== $relation) { - $relation['$id'] = ID::unique(); - $relation = new Document($relation); - } else { + } elseif (!(\is_array($relation) && \array_values($relation) !== $relation)) { throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, 'Relationship value must be an object or document ID string, not ' . \gettype($relation)); } @@ -276,8 +271,6 @@ abstract class Action extends DatabasesAction throw new Exception(Exception::GENERAL_BAD_REQUEST, $validator->getDescription()); } } - - return [$relationId, $relation]; } /** diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php index 5244efc2ab..8927c6b27b 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php @@ -309,7 +309,12 @@ class Create extends Action ); foreach ($relations as &$relation) { - [$relationId, $relation] = $this->validateRelationship($relation); + $this->validateRelationship($relation); + + if (\is_array($relation) && \array_values($relation) !== $relation) { + $relation['$id'] = ID::unique(); + $relation = new Document($relation); + } if ($relation instanceof Document) { $relation = $this->removeReadonlyAttributes($relation, $isAPIKey || $isPrivilegedUser); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php index f6fa6a95cc..0f3eb9e026 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php @@ -201,7 +201,12 @@ class Update extends Action ); foreach ($relations as &$relation) { - [$relationId, $relation] = $this->validateRelationship($relation); + $this->validateRelationship($relation); + + if (\is_array($relation) && \array_values($relation) !== $relation) { + $relation['$id'] = ID::unique(); + $relation = new Document($relation); + } if ($relation instanceof Document) { $relation = $this->removeReadonlyAttributes($relation, $isAPIKey || $isPrivilegedUser); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php index 9cc38050c4..cdff15c5ab 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php @@ -211,7 +211,12 @@ class Upsert extends Action ); foreach ($relations as &$relation) { - [$relationId, $relation] = $this->validateRelationship($relation); + $this->validateRelationship($relation); + + if (\is_array($relation) && \array_values($relation) !== $relation) { + $relation['$id'] = ID::unique(); + $relation = new Document($relation); + } if ($relation instanceof Document) { $relation = $this->removeReadonlyAttributes($relation, $isAPIKey || $isPrivilegedUser);