diff --git a/src/Command/ControllerCommand.php b/src/Command/ControllerCommand.php index 2b88cf0f..82ca60d0 100644 --- a/src/Command/ControllerCommand.php +++ b/src/Command/ControllerCommand.php @@ -129,6 +129,12 @@ public function bake(string $controllerName, Arguments $args, ConsoleIo $io): vo $singularHumanName = $this->_singularHumanName($controllerName); $pluralHumanName = $this->_variableName($controllerName); + // Handle cases where singular and plural are identical (e.g., "news", "sheep") + // to avoid variable collisions in generated controller code + if ($singularName === $pluralName) { + $singularName .= 'Entity'; + } + $defaultModel = sprintf('%s\Model\Table\%sTable', $namespace, $controllerName); if (!class_exists($defaultModel)) { $defaultModel = null; diff --git a/src/Command/TemplateCommand.php b/src/Command/TemplateCommand.php index 239eb576..64619f00 100644 --- a/src/Command/TemplateCommand.php +++ b/src/Command/TemplateCommand.php @@ -325,6 +325,12 @@ protected function _loadController(ConsoleIo $io): array $pluralVar = Inflector::variable($this->controllerName); $pluralHumanName = $this->_pluralHumanName($this->controllerName); + // Handle cases where singular and plural are identical (e.g., "news", "sheep") + // to avoid generating invalid code like `foreach ($news as $news)` + if ($singularVar === $pluralVar) { + $singularVar .= 'Entity'; + } + return compact( 'modelObject', 'modelClass', diff --git a/templates/bake/Template/view.twig b/templates/bake/Template/view.twig index e4234c0e..a6841f35 100644 --- a/templates/bake/Template/view.twig +++ b/templates/bake/Template/view.twig @@ -120,6 +120,9 @@ {% set relations = associations.BelongsToMany|merge(associations.HasMany) %} {% for alias, details in relations %} {%~ set otherSingularVar = alias|singularize|variable %} + {%~ if otherSingularVar == singularVar %} + {%~ set otherSingularVar = otherSingularVar ~ 'Related' %} + {%~ endif %} {%~ set otherPluralHumanName = details.controller|underscore|humanize %}