From 5c74ff709e247808b7c68bff866ae89f3f0e1b46 Mon Sep 17 00:00:00 2001 From: Michael Martins Date: Fri, 22 May 2026 15:06:57 -0300 Subject: [PATCH 1/2] Preserve sub-namespace when generating Entity from Model (--return=entity) --- system/Commands/Generators/ModelGenerator.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/system/Commands/Generators/ModelGenerator.php b/system/Commands/Generators/ModelGenerator.php index 5450bda79b4c..30b7422238fd 100644 --- a/system/Commands/Generators/ModelGenerator.php +++ b/system/Commands/Generators/ModelGenerator.php @@ -114,18 +114,23 @@ protected function prepare(string $class): string } if ($return === 'entity') { - $return = str_replace('Models', 'Entities', $class); + // Build the fully-qualified entity class from the model class so + // that the generated Entity keeps any sub-namespaces (eg. Admin). + $entityClass = str_replace('Models', 'Entities', $class); - if (preg_match('/^(\S+)Model$/i', $return, $match) === 1) { - $return = $match[1]; + if (preg_match('/^(\S+)Model$/i', $entityClass, $match) === 1) { + $entityClass = $match[1]; if ($this->getOption('suffix')) { - $return .= 'Entity'; + $entityClass .= 'Entity'; } } - $return = '\\' . trim($return, '\\') . '::class'; - $this->call('make:entity', array_merge([$baseClass], $this->params)); + // Call the entity generator with the fully-qualified class name so + // it ends up under the correct sub-namespace/folder (eg. Admin). + $this->call('make:entity', array_merge([trim($entityClass, '\\')], $this->params)); + + $return = '\\' . trim($entityClass, '\\') . '::class'; } else { $return = "'{$return}'"; } From b618dcf8826425bbf6fc29b5c6aa9b5fd36a6564 Mon Sep 17 00:00:00 2001 From: Michael Martins Date: Sun, 24 May 2026 16:40:12 -0300 Subject: [PATCH 2/2] test: add test for preserving sub-namespace in entity generation --- .../Generators/ModelGeneratorTest.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/system/Commands/Generators/ModelGeneratorTest.php b/tests/system/Commands/Generators/ModelGeneratorTest.php index 520bbbdc7a3b..8dab8b007f19 100644 --- a/tests/system/Commands/Generators/ModelGeneratorTest.php +++ b/tests/system/Commands/Generators/ModelGeneratorTest.php @@ -134,6 +134,33 @@ public function testGenerateModelWithOptionSuffix(): void rmdir(dirname($entity)); } + public function testGenerateModelWithSubNamespaceAndReturnEntity(): void + { + command('make:model admin/class --return entity'); + + $model = APPPATH . 'Models/Admin/Class.php'; + $entity = APPPATH . 'Entities/Admin/Class.php'; + + $this->assertFileExists($model); + $this->assertFileExists($entity); + + if (is_file($model)) { + unlink($model); + } + $modelDir = dirname($model); + if (is_dir($modelDir)) { + rmdir($modelDir); + } + + if (is_file($entity)) { + unlink($entity); + } + $entityDir = dirname($entity); + if (is_dir($entityDir)) { + rmdir($entityDir); + } + } + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/5050 */