-
Notifications
You must be signed in to change notification settings - Fork 33
Description
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:
- The registry alias becomes "Administratives" (the association name) instead of "People"
- The resulting route "CrudJsonApi.Administratives:Administratives" doesn't exist
- 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" consistentlyThis works because:
_getRepositoryRoutingParameters()returns the correct controller name regardless of relationship type- For regular relationships, it produces the same result as before
- For self-referential relationships, it fixes the registry alias confusion
- 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.