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

Self-referential relationships generate incorrect route names #189

Open
geoidesic opened this issue Jan 20, 2025 · 0 comments
Open

Self-referential relationships generate incorrect route names #189

geoidesic opened this issue Jan 20, 2025 · 0 comments
Labels

Comments

@geoidesic
Copy link
Collaborator

Description

When using self-referential relationships in CakePHP with the JSON API plugin, the route name generation fails because the registry alias is incorrectly set to the association name instead of the source table name.

Example

Given a People table with a self-referential relationship named Administratives:

// In PeopleTable.php
$this->belongsToMany('Administratives', [
'className' => 'People',
// ... other options
]);

The plugin attempts to generate a route name using:

$from = $this->getRepository()->getRegistryAlias(); // Returns "Administratives"
$type = $association->getName(); // Returns "Administratives"
$routeName = "CrudJsonApi.{$from}:{$type}"; // Results in "CrudJsonApi.Administratives:Administratives"

This fails because:

  1. The registry alias becomes "Administratives" (the association name) instead of "People"
  2. The resulting route "CrudJsonApi.Administratives:Administratives" doesn't exist
  3. The correct route should be "CrudJsonApi.People:Administratives"

Current Behavior

  • Regular relationships work correctly (e.g., Enquiries -> People as Administratives)
  • Self-referential relationships fail with MissingRouteException

Expected Behavior

Both regular and self-referential relationships should generate correct route names using the controller name rather than the potentially confused registry alias.

Fix

Use the controller name from routing parameters instead of the registry alias:

['controller' => $controllerName] = $this->getRepositoryRoutingParameters($this->getRepository());
$from = $controllerName; // Returns "People" consistently

This works because:

  1. _getRepositoryRoutingParameters() returns the correct controller name regardless of relationship type
  2. For regular relationships, it produces the same result as before
  3. For self-referential relationships, it fixes the registry alias confusion
  4. It respects plugin prefixes and custom resource paths

Additional Context

This issue only affects self-referential relationships where the association name differs from the table name. The fix maintains compatibility with all other relationship types while correctly handling the self-referential case.

@geoidesic geoidesic added the bug label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant