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

Throw exception when setting association with duplicate alias. #17676

Merged
merged 3 commits into from
May 3, 2024

Conversation

ADmad
Copy link
Member

@ADmad ADmad commented May 1, 2024

This is quite a common mistake done by new users (when creating multiple associations to the same table) and the exception should save them lot of debugging and support requests.

@ADmad ADmad added this to the 5.1.0 milestone May 1, 2024
@ADmad ADmad force-pushed the 5.1-duplicate-association-alias branch from a43f933 to ba1d54a Compare May 1, 2024 13:35
@@ -131,6 +131,10 @@ protected function setupAssociations(): void
$conditions[$name . '.content !='] = '';
}

if ($this->table->associations()->has($name)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

These checks are mainly required because in our test suite we remove and re-add the Translate behavior with different configs but removing the behavior doesn't remove the associations created on the source table.

@ADmad ADmad force-pushed the 5.1-duplicate-association-alias branch from ba1d54a to 01e78f5 Compare May 1, 2024 13:42
@ADmad
Copy link
Member Author

ADmad commented May 1, 2024

Someone needs to check why MySQL is not accessible for the GA.

@ADmad ADmad force-pushed the 5.1-duplicate-association-alias branch from 1027609 to e679478 Compare May 1, 2024 14:31
@ADmad ADmad force-pushed the 5.1-duplicate-association-alias branch from e679478 to 7ab35e0 Compare May 1, 2024 14:34
@ADmad
Copy link
Member Author

ADmad commented May 1, 2024

Someone needs to check why MySQL is not accessible for the GA.

Ubuntu 22.04 has MySQL 8, so using docker to get the latest image, to test stuff like window functions, isn't necessary right?

@@ -461,7 +461,7 @@ public function testAttachToFormatResultsNoDirtyResults(): void
{
$this->setAppNamespace('TestApp');
$articles = $this->getTableLocator()->get('Articles');
$articles->belongsTo('Authors')
Copy link
Member

Choose a reason for hiding this comment

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

Not sure users would ever have this code in their table init, but I think the error makes sense in this situation.

@othercorey
Copy link
Member

Ubuntu 22.04 has MySQL 8, so using docker to get the latest image, to test stuff like window functions, isn't necessary right?

I think so. If we supported multiple versions, then we might want to keep the wait loop.

Co-authored-by: othercorey <corey.taylor.fl@gmail.com>
@ADmad
Copy link
Member Author

ADmad commented May 1, 2024

If we supported multiple versions, then we might want to keep the wait loop.

Let's keep things simple, we can switch back to using docker if/when needed.

@@ -78,6 +80,10 @@ public function add(string $alias, Association $association): Association
{
[, $alias] = pluginSplit($alias);

if (isset($this->_items[$alias])) {
throw new CakeException(sprintf('Association alias `%s` is already set.', $alias));
Copy link
Member

Choose a reason for hiding this comment

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

Generally I'm in favour of this, but we should be prepared for this to impact userland plugins a bit.

Copy link
Member

Choose a reason for hiding this comment

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

Do plugins stomp each other? Do we need to allow plugins to set if they're stompable? That would depend on order of load so maybe it's a useless concept.

Copy link
Member

Choose a reason for hiding this comment

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

They could and that likely means it is happening somewhere in the wild. If this becomes a blocker we can always revisit it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am willing to risk a few support tickets, if things get too bad we can always revert :)

Copy link
Member Author

@ADmad ADmad May 2, 2024

Choose a reason for hiding this comment

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

Based on the queries I have seen on our support channels, hidden duplication/overwriting causes a lot of grief to users.

Copy link
Member

Choose a reason for hiding this comment

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

ok, I'm onboard with the change 👍

@markstory markstory merged commit 94f4b37 into 5.next May 3, 2024
10 checks passed
@markstory markstory deleted the 5.1-duplicate-association-alias branch May 3, 2024 13:51
markstory added a commit to cakephp/docs that referenced this pull request May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants