diff --git a/CHANGELOG.md b/CHANGELOG.md index 138b22212..62c421482 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 gii extension Change Log 2.0.8 under development ----------------------- -- no changes in this release. +- Enh #366: Better class and file names for uppercase tables (slinstj) 2.0.7 May 3, 2018 diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 89edd8be8..61f764400 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -880,7 +880,9 @@ protected function generateClassName($tableName, $useSchemaName = null) } } - return $this->classNames[$fullTableName] = Inflector::id2camel($schemaName.$className, '_'); + $schemaName = ctype_upper(strtr($schemaName, ['_' => '', '-' => ''])) ? strtolower($schemaName) : $schemaName; + $className = ctype_upper(strtr($className, ['_' => '', '-' => ''])) ? strtolower($className) : $className; + return $this->classNames[$fullTableName] = strtr(ucwords(implode(' ', explode('_', strtolower(strtr(Inflector::camel2words($schemaName.$className), [' ' => '_']))))), [' ' => '']); } /** diff --git a/tests/generators/ModelGeneratorMock.php b/tests/generators/ModelGeneratorMock.php new file mode 100644 index 000000000..14d6c228d --- /dev/null +++ b/tests/generators/ModelGeneratorMock.php @@ -0,0 +1,18 @@ +generateClassName($tableName, $useSchemaName); + } +} diff --git a/tests/generators/ModelGeneratorTest.php b/tests/generators/ModelGeneratorTest.php index 20e598a91..ac83de25c 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -214,4 +214,27 @@ public function testRules($tableName, $fileName, $rules) ); } } + + public function testGenerateClassName() + { + $modelGenerator = new ModelGeneratorMock; + + $tableNames = [ + 'lower_underline_name' => 'LowerUnderlineName', + 'Ucwords_Underline_Name' => 'UcwordsUnderlineName', + 'UPPER_UNDERLINE_NAME' => 'UpperUnderlineName', + 'lower-hyphen-name' => 'LowerHyphenName', + 'Ucwords-Hyphen-Name' => 'UcwordsHyphenName', + 'UPPER-HYPHEN-NAME' => 'UpperHyphenName', + 'CamelCaseName' => 'CamelCaseName', + 'lowerUcwordsName' => 'LowerUcwordsName', + 'lowername' => 'Lowername', + 'UPPERNAME' => 'Uppername', + ]; + + foreach ($tableNames as $tableName => $expectedClassName) { + $generatedClassName = $modelGenerator->publicGenerateClassName($tableName); + $this->assertEquals($generatedClassName, $expectedClassName); + } + } }