From dc55d41c64c4486928427b2f61c2af4a4de63fd0 Mon Sep 17 00:00:00 2001 From: Samuel Asor <8720569+sammyskills@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:52:16 +0100 Subject: [PATCH 1/4] add dbgroup to model template only when specified as an option --- system/Commands/Generators/ModelGenerator.php | 10 ++++++---- system/Commands/Generators/Views/model.tpl.php | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/system/Commands/Generators/ModelGenerator.php b/system/Commands/Generators/ModelGenerator.php index b4b7ffcda2ca..1e17b6639f85 100644 --- a/system/Commands/Generators/ModelGenerator.php +++ b/system/Commands/Generators/ModelGenerator.php @@ -101,9 +101,11 @@ protected function prepare(string $class): string $baseClass = $match[1]; } - $table = is_string($table) ? $table : plural(strtolower($baseClass)); - $dbGroup = is_string($dbGroup) ? $dbGroup : 'default'; - $return = is_string($return) ? $return : 'array'; + // Add or remove the DBGroup line based on the presence of the dbgroup option + $addDBGroupLine = is_string($dbGroup) ? true : false; + + $table = is_string($table) ? $table : plural(strtolower($baseClass)); + $return = is_string($return) ? $return : 'array'; if (! in_array($return, ['array', 'object', 'entity'], true)) { // @codeCoverageIgnoreStart @@ -129,6 +131,6 @@ protected function prepare(string $class): string $return = "'{$return}'"; } - return $this->parseTemplate($class, ['{table}', '{dbGroup}', '{return}'], [$table, $dbGroup, $return]); + return $this->parseTemplate($class, ['{dbGroup}', '{table}', '{return}'], [$dbGroup, $table, $return], compact('addDBGroupLine')); } } diff --git a/system/Commands/Generators/Views/model.tpl.php b/system/Commands/Generators/Views/model.tpl.php index ca6d0ec8b954..7bf7676f320f 100644 --- a/system/Commands/Generators/Views/model.tpl.php +++ b/system/Commands/Generators/Views/model.tpl.php @@ -6,7 +6,9 @@ class {class} extends Model { + protected $DBGroup = '{dbGroup}'; + protected $table = '{table}'; protected $primaryKey = 'id'; protected $useAutoIncrement = true; From fc8b0364f2d4b08159aadfa70aaed4d41894ef21 Mon Sep 17 00:00:00 2001 From: Samuel Asor <8720569+sammyskills@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:00:42 +0100 Subject: [PATCH 2/4] applied rector fix --- system/Commands/Generators/ModelGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Commands/Generators/ModelGenerator.php b/system/Commands/Generators/ModelGenerator.php index 1e17b6639f85..cb22ffbbf963 100644 --- a/system/Commands/Generators/ModelGenerator.php +++ b/system/Commands/Generators/ModelGenerator.php @@ -102,7 +102,7 @@ protected function prepare(string $class): string } // Add or remove the DBGroup line based on the presence of the dbgroup option - $addDBGroupLine = is_string($dbGroup) ? true : false; + $addDBGroupLine = is_string($dbGroup); $table = is_string($table) ? $table : plural(strtolower($baseClass)); $return = is_string($return) ? $return : 'array'; From 10815db9eb352de5f0aa9c0ce2fc44eeab433a6c Mon Sep 17 00:00:00 2001 From: Samuel Asor <8720569+sammyskills@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:05:33 +0100 Subject: [PATCH 3/4] fixed phpunit failure --- tests/system/Commands/ModelGeneratorTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/system/Commands/ModelGeneratorTest.php b/tests/system/Commands/ModelGeneratorTest.php index e6ca050d553b..e1b3c4ae354a 100644 --- a/tests/system/Commands/ModelGeneratorTest.php +++ b/tests/system/Commands/ModelGeneratorTest.php @@ -52,7 +52,6 @@ public function testGenerateModel(): void $this->assertFileExists($file); $this->assertStringContainsString('extends Model', $this->getFileContent($file)); $this->assertStringContainsString('protected $table = \'users\';', $this->getFileContent($file)); - $this->assertStringContainsString('protected $DBGroup = \'default\';', $this->getFileContent($file)); $this->assertStringContainsString('protected $returnType = \'array\';', $this->getFileContent($file)); } From 23eb997a61730857e7e4375aceae76b3f953c717 Mon Sep 17 00:00:00 2001 From: Samuel Asor <8720569+sammyskills@users.noreply.github.com> Date: Tue, 24 Oct 2023 09:16:28 +0100 Subject: [PATCH 4/4] applied code review suggestion --- system/Commands/Generators/ModelGenerator.php | 5 +---- system/Commands/Generators/Views/model.tpl.php | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/system/Commands/Generators/ModelGenerator.php b/system/Commands/Generators/ModelGenerator.php index cb22ffbbf963..eb0a1aa9802f 100644 --- a/system/Commands/Generators/ModelGenerator.php +++ b/system/Commands/Generators/ModelGenerator.php @@ -101,9 +101,6 @@ protected function prepare(string $class): string $baseClass = $match[1]; } - // Add or remove the DBGroup line based on the presence of the dbgroup option - $addDBGroupLine = is_string($dbGroup); - $table = is_string($table) ? $table : plural(strtolower($baseClass)); $return = is_string($return) ? $return : 'array'; @@ -131,6 +128,6 @@ protected function prepare(string $class): string $return = "'{$return}'"; } - return $this->parseTemplate($class, ['{dbGroup}', '{table}', '{return}'], [$dbGroup, $table, $return], compact('addDBGroupLine')); + return $this->parseTemplate($class, ['{dbGroup}', '{table}', '{return}'], [$dbGroup, $table, $return], compact('dbGroup')); } } diff --git a/system/Commands/Generators/Views/model.tpl.php b/system/Commands/Generators/Views/model.tpl.php index 7bf7676f320f..b9b9c99eb560 100644 --- a/system/Commands/Generators/Views/model.tpl.php +++ b/system/Commands/Generators/Views/model.tpl.php @@ -6,7 +6,7 @@ class {class} extends Model { - + protected $DBGroup = '{dbGroup}'; protected $table = '{table}';