Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -5998,6 +5998,10 @@ private function createDocumentRelationships(Document $collection, Document $doc
try {
switch (\gettype($value)) {
case 'array':

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this throw in case of array? Maybe should be Object (Document)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it should. The child side is always invalid for one-way relationships, so arrays should hit the same error too. We only see an array here because that’s what reaches this code path.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be precise, this only applies to one-way one-to-one. The Document case already throws the same error. The reason its an array here is that the child key isnt treated as an attribute, so it never gets converted to a Document. 🤔

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) ||
Expand Down
28 changes: 28 additions & 0 deletions tests/e2e/Adapter/Scopes/RelationshipTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Loading