Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Same twoWayKey #327

Closed
wants to merge 3 commits into from
Closed
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
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 41 additions & 12 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -3429,6 +3429,47 @@ public function testNoChangeUpdateDocumentWithoutPermission(Document $document):
return $document;
}

public function testRelationSametwoWayKey(): void
{
if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) {
$this->expectNotToPerformAssertions();
return;
}

$permissions = [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::delete(Role::any()),
];

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

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

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

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

public function testNoChangeUpdateDocumentWithRelationWithoutPermission(): void
{
if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) {
Expand Down Expand Up @@ -5585,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
Loading