diff --git a/src/Database/Database.php b/src/Database/Database.php index d7f1a01b2..d581529fa 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -5998,6 +5998,10 @@ private function createDocumentRelationships(Document $collection, Document $doc try { switch (\gettype($value)) { case 'array': + if ($relationType === Database::RELATION_ONE_TO_ONE && !$twoWay && $side === Database::RELATION_SIDE_CHILD) { + throw new RelationshipException('Invalid relationship value. Cannot set a value from the child side of a oneToOne relationship when twoWay is false.'); + } + if ( ($relationType === Database::RELATION_MANY_TO_ONE && $side === Database::RELATION_SIDE_PARENT) || ($relationType === Database::RELATION_ONE_TO_MANY && $side === Database::RELATION_SIDE_CHILD) || diff --git a/tests/e2e/Adapter/Scopes/RelationshipTests.php b/tests/e2e/Adapter/Scopes/RelationshipTests.php index 2b162b238..dbfba7bfc 100644 --- a/tests/e2e/Adapter/Scopes/RelationshipTests.php +++ b/tests/e2e/Adapter/Scopes/RelationshipTests.php @@ -2185,6 +2185,34 @@ public function testCreateInvalidArrayIntValueRelationship(): void ])); } + public function testCreateInvalidOneWayChildArrayValueRelationship(): void + { + /** @var Database $database */ + $database = $this->getDatabase(); + + if (!$database->getAdapter()->getSupportForRelationships()) { + $this->expectNotToPerformAssertions(); + return; + } + + $database->createCollection('reverse1'); + $database->createCollection('reverse2'); + + $database->createRelationship( + collection: 'reverse1', + relatedCollection: 'reverse2', + type: Database::RELATION_ONE_TO_ONE, + ); + + $this->expectException(RelationshipException::class); + $this->expectExceptionMessage('Invalid relationship value. Cannot set a value from the child side of a oneToOne relationship when twoWay is false.'); + + $database->createDocument('reverse2', new Document([ + '$id' => ID::unique(), + 'reverse1' => ['name' => 'reverse'], + ])); + } + public function testCreateEmptyValueRelationship(): void { /** @var Database $database */