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 efd3f4ed6f..c43c6114ef 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 @@ -251,7 +251,8 @@ abstract class Action extends DatabasesAction } /** - * Validate a relationship value and its document ID. + * Validate relationship values. + * Handles Document objects, ID strings, and associative arrays. */ protected function validateRelationship(mixed $relation): void { @@ -263,8 +264,6 @@ abstract class Action extends DatabasesAction $relationId = $relation; } elseif (\is_array($relation) && \array_values($relation) !== $relation) { $relationId = $relation['$id'] ?? null; - } else { - throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, 'Relationship value must be an object or document ID string, not ' . \gettype($relation)); } if ($relationId !== null) { 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 253cf8ec3c..eebe59796e 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,22 +309,17 @@ class Create extends Action ); foreach ($relations as &$relation) { - // Generate unique ID for new relation without $id if ( \is_array($relation) && \array_values($relation) !== $relation && !isset($relation['$id']) ) { $relation['$id'] = ID::unique(); + $relation = new Document($relation); } $this->validateRelationship($relation); - // If the relation is an array it can be either update or create a child document. - if (\is_array($relation) && \array_values($relation) !== $relation) { - $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 34f2a45e15..ff3ab6e23c 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,22 +201,18 @@ class Update extends Action ); foreach ($relations as &$relation) { - // Generate unique ID for new relation without $id + // If the relation is an array it can be either update or create a child document. if ( \is_array($relation) && \array_values($relation) !== $relation && !isset($relation['$id']) ) { $relation['$id'] = ID::unique(); + $relation = new Document($relation); } $this->validateRelationship($relation); - // If the relation is an array it can be either update or create a child document. - if (\is_array($relation) && \array_values($relation) !== $relation) { - $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 8b500a9e61..d0536b65ef 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,22 +211,18 @@ class Upsert extends Action ); foreach ($relations as &$relation) { - // Generate unique ID for new relation without $id + // If the relation is an array it can be either update or create a child document. if ( \is_array($relation) && \array_values($relation) !== $relation && !isset($relation['$id']) ) { $relation['$id'] = ID::unique(); + $relation = new Document($relation); } $this->validateRelationship($relation); - // If the relation is an array it can be either update or create a child document. - if (\is_array($relation) && \array_values($relation) !== $relation) { - $relation = new Document($relation); - } - if ($relation instanceof Document) { $relation = $this->removeReadonlyAttributes($relation, $isAPIKey || $isPrivilegedUser);