Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Related attribute already exists Try Catch
  • Loading branch information
fogelito committed Oct 2, 2023
commit 217babece9b38d62e8a318b7c826ec3925166887
16 changes: 10 additions & 6 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,7 @@ public function createRelationship(

$id ??= $relatedCollection->getId();

$twoWayKey ??= $collection->getId() . '_' . uniqid();
$twoWayKey ??= $collection->getId();

$attributes = $collection->getAttribute('attributes', []);
/** @var Document[] $attributes */
Expand All @@ -1537,11 +1537,15 @@ public function createRelationship(
throw new DuplicateException('Attribute already exists');
}

if ($attribute->getAttribute('type') === self::VAR_RELATIONSHIP
&& \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey)
&& $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId()
) {
throw new DuplicateException('Related attribute already exists');
try {
if ($attribute->getAttribute('type') === self::VAR_RELATIONSHIP
&& \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey)
&& $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId()
) {
throw new DuplicateException('Related attribute already exists');
}
} catch (DuplicateException $e) {
$twoWayKey ??= $collection->getId() . '_' . uniqid();
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be $twoWayKey = $collection->getId() . '_' . uniqid();, otherwise it will only assign if null

}
Comment on lines +1540 to 1549
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need a try/catch here, we can use use the if:

Suggested change
try {
if ($attribute->getAttribute('type') === self::VAR_RELATIONSHIP
&& \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey)
&& $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId()
) {
throw new DuplicateException('Related attribute already exists');
}
} catch (DuplicateException $e) {
$twoWayKey ??= $collection->getId() . '_' . uniqid();
}
if ($attribute->getAttribute('type') === self::VAR_RELATIONSHIP
&& \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey)
&& $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId()
) {
$twoWayKey ??= $collection->getId() . '_' . uniqid();
}

}

Expand Down
36 changes: 12 additions & 24 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -3429,7 +3429,7 @@ public function testNoChangeUpdateDocumentWithoutPermission(Document $document):
return $document;
}

public function testRelationSameKey(): void
public function testRelationSametwoWayKey(): void
{
if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) {
$this->expectNotToPerformAssertions();
Expand All @@ -3442,30 +3442,30 @@ public function testRelationSameKey(): void
Permission::delete(Role::any()),
];

static::getDatabase()->createCollection("c1", [], [], $permissions);
static::getDatabase()->createCollection("c2", [], [], $permissions);
static::getDatabase()->createCollection('c1', [], [], $permissions);
static::getDatabase()->createCollection('c2', [], [], $permissions);

$res = static::getDatabase()->createRelationship(
collection: "c1",
relatedCollection: "c2",
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_ONE_TO_ONE,
id: "c2"
id: 'c2'
);
$this->assertTrue($res);

$res = static::getDatabase()->createRelationship(
collection: "c1",
relatedCollection: "c2",
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_ONE_TO_MANY,
id: "c1"
id: 'c1'
);
$this->assertTrue($res);

$res = static::getDatabase()->createRelationship(
collection: "c1",
relatedCollection: "c2",
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_MANY_TO_ONE,
id: "c3"
id: 'c3'
);
$this->assertTrue($res);
}
Expand Down Expand Up @@ -5626,18 +5626,6 @@ public function testIdenticalTwoWayKeyRelationship(): void
id: 'child1'
);

try {
static::getDatabase()->createRelationship(
collection: 'parent',
relatedCollection: 'child',
type: Database::RELATION_ONE_TO_MANY,
id: 'children',
);
$this->fail('Failed to throw Exception');
} catch (Exception $e) {
$this->assertEquals('Related attribute already exists', $e->getMessage());
}

static::getDatabase()->createRelationship(
collection: 'parent',
relatedCollection: 'child',
Expand Down