Skip to content

Commit 32eeb5d

Browse files
authored
fix: preserve sub-namespace when generating Entity from Model (i.e., --return entity) (#10232)
* Preserve sub-namespace when generating Entity from Model (--return=entity) * test: add test for preserving sub-namespace in entity generation * docs: add changelog entry for #10232
1 parent fc45198 commit 32eeb5d

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

system/Commands/Generators/ModelGenerator.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,23 @@ protected function prepare(string $class): string
114114
}
115115

116116
if ($return === 'entity') {
117-
$return = str_replace('Models', 'Entities', $class);
117+
// Build the fully-qualified entity class from the model class so
118+
// that the generated Entity keeps any sub-namespaces (eg. Admin).
119+
$entityClass = str_replace('Models', 'Entities', $class);
118120

119-
if (preg_match('/^(\S+)Model$/i', $return, $match) === 1) {
120-
$return = $match[1];
121+
if (preg_match('/^(\S+)Model$/i', $entityClass, $match) === 1) {
122+
$entityClass = $match[1];
121123

122124
if ($this->getOption('suffix')) {
123-
$return .= 'Entity';
125+
$entityClass .= 'Entity';
124126
}
125127
}
126128

127-
$return = '\\' . trim($return, '\\') . '::class';
128-
$this->call('make:entity', array_merge([$baseClass], $this->params));
129+
// Call the entity generator with the fully-qualified class name so
130+
// it ends up under the correct sub-namespace/folder (eg. Admin).
131+
$this->call('make:entity', array_merge([trim($entityClass, '\\')], $this->params));
132+
133+
$return = '\\' . trim($entityClass, '\\') . '::class';
129134
} else {
130135
$return = "'{$return}'";
131136
}

tests/system/Commands/Generators/ModelGeneratorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,33 @@ public function testGenerateModelWithOptionSuffix(): void
134134
rmdir(dirname($entity));
135135
}
136136

137+
public function testGenerateModelWithSubNamespaceAndReturnEntity(): void
138+
{
139+
command('make:model admin/class --return entity');
140+
141+
$model = APPPATH . 'Models/Admin/Class.php';
142+
$entity = APPPATH . 'Entities/Admin/Class.php';
143+
144+
$this->assertFileExists($model);
145+
$this->assertFileExists($entity);
146+
147+
if (is_file($model)) {
148+
unlink($model);
149+
}
150+
$modelDir = dirname($model);
151+
if (is_dir($modelDir)) {
152+
rmdir($modelDir);
153+
}
154+
155+
if (is_file($entity)) {
156+
unlink($entity);
157+
}
158+
$entityDir = dirname($entity);
159+
if (is_dir($entityDir)) {
160+
rmdir($entityDir);
161+
}
162+
}
163+
137164
/**
138165
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5050
139166
*/

user_guide_src/source/changelogs/v4.7.4.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Bugs Fixed
3131
**********
3232

3333
- **API:** Fixed a bug in Transformers where the root request's ``fields`` and ``include`` query parameters leaked into nested transformers created inside ``include*()`` methods, causing incorrect field filtering, unexpected includes, or infinite recursion.
34+
- **Commands:** Fixed a bug where ``make:model --return entity`` did not preserve sub-namespaces when generating the related Entity class.
3435
- **Commands:** Fixed a bug where ``spark lang:find`` treated translation keys already provided by the framework or another namespace (such as ``Errors.*`` in ``system/Language``) as new, listing them under ``--show-new`` and writing untranslated placeholders into ``app/Language`` that overrode the existing translations.
3536
- **Database:** Fixed a bug where ``updateBatch()`` could be called after Query Builder ``where()`` conditions, even though it's not supported. In this situation, now the ``DatabaseException`` is thrown.
3637
- **Filters:** Fixed a bug in ``InvalidChars`` filter where invalid UTF-8 or control characters in array keys were not checked.

0 commit comments

Comments
 (0)