From d25e7bf93ef670631afa16bfbf5ae8fc06c2b767 Mon Sep 17 00:00:00 2001 From: uldisn Date: Sun, 24 May 2020 23:09:07 +0300 Subject: [PATCH 01/19] Implemented enum --- src/generators/model/Generator.php | 73 ++++++++++++++++++++++++- src/generators/model/default/model.php | 72 ++++++++++++++++++++++++ tests/bootstrap.php | 6 +- tests/generators/ModelGeneratorTest.php | 23 ++++++++ 4 files changed, 170 insertions(+), 4 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 3d1abf75a..5d136feee 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -12,6 +12,7 @@ use yii\base\NotSupportedException; use yii\db\ActiveQuery; use yii\db\ActiveRecord; +use yii\db\ColumnSchema; use yii\db\Connection; use yii\db\Exception; use yii\db\Schema; @@ -245,6 +246,7 @@ public function generate() 'rules' => $this->generateRules($tableSchema), 'relations' => $tableRelations, 'relationsClassHints' => $this->generateRelationsClassHints($tableRelations, $this->generateQuery), + 'enum' => $this->getEnum($tableSchema->columns), ]; $files[] = new CodeFile( Yii::getAlias('@' . str_replace('\\', '/', $this->ns)) . '/' . $modelClassName . '.php', @@ -429,6 +431,17 @@ public function generateRules($table) $rules[] = "[['" . implode("', '", $columns) . "'], 'string', 'max' => $length]"; } + foreach ($this->getEnum($table->columns) as $field_name => $field_details) { + $fieldEnumValues = []; + foreach ($field_details['values'] as $field_enum_values) { + $fieldEnumValues[] = 'self::'.$field_enum_values['const_name']; + } + $rules['enum-' . $field_name] = "['".$field_name."', 'in', 'range' => [\n ".implode( + ",\n ", + $fieldEnumValues + ).",\n ]\n ]"; + } + $db = $this->getDbConnection(); // Unique indexes rules @@ -490,7 +503,7 @@ private function generateManyManyRelations($table, $fks, $relations) $db = $this->getDbConnection(); foreach ($fks as $pair) { - list($firstKey, $secondKey) = $pair; + [$firstKey, $secondKey] = $pair; $table0 = $firstKey[0][0]; $table1 = $secondKey[0][0]; unset($firstKey[0][0], $secondKey[0][0]); @@ -1094,4 +1107,62 @@ protected function isColumnAutoIncremental($table, $columns) return false; } + + /** + * prepare ENUM field values. + * + * @param ColumnSchema[] $columns + * + * @return array + */ + protected function getEnum($columns) + { + $enum = []; + foreach ($columns as $column) { + if (!$this->isEnum($column)) { + continue; + } + + $column_camel_name = str_replace(' ', '', ucwords(implode(' ', explode('_', $column->name)))); + $enum[$column->name]['func_opts_name'] = 'opts'.$column_camel_name; + $enum[$column->name]['func_get_label_name'] = 'get'.$column_camel_name.'ValueLabel'; + $enum[$column->name]['isFunctionPrefix'] = 'is'.$column_camel_name; + $enum[$column->name]['getFunctionPrefix'] = 'get'.$column_camel_name; + $enum[$column->name]['columnName'] = $column->name; + $enum[$column->name]['values'] = []; + + $enum_values = explode(',', substr($column->dbType, 4, strlen($column->dbType) - 1)); + + foreach ($enum_values as $value) { + $value = trim($value, "()'"); + + $const_name = strtoupper($column->name.'_'.$value); + $const_name = preg_replace('/\s+/', '_', $const_name); + $const_name = str_replace(['-', '_', ' '], '_', $const_name); + $const_name = preg_replace('/[^A-Z0-9_]/', '', $const_name); + + $label = Inflector::camel2words($value); + + $enum[$column->name]['values'][] = [ + 'value' => $value, + 'const_name' => $const_name, + 'label' => $label, + 'isFunctionSuffix' => str_replace(' ', '', ucwords(implode(' ', explode('_', $value)))) + ]; + } + } + + return $enum; + } + + /** + * validate is ENUM column. + * + * @param ColumnSchema $column + * @return string + */ + protected function isEnum($column) + { + return stripos(strtoupper($column->dbType), 'ENUM') === 0; + } } diff --git a/src/generators/model/default/model.php b/src/generators/model/default/model.php index ed101a884..8abb85e80 100644 --- a/src/generators/model/default/model.php +++ b/src/generators/model/default/model.php @@ -13,6 +13,7 @@ /* @var $labels string[] list of attribute labels (name => label) */ /* @var $rules string[] list of validation rules */ /* @var $relations array list of relations (name => relation declaration) */ +/* @var array $enum list of ENUM fields */ echo " @@ -36,6 +37,22 @@ */ class extends baseClass, '\\') . "\n" ?> { + + + /** + * ENUM field values + */ + $column_data){ + foreach ($column_data['values'] as $enum_value){ + echo ' const ' . $enum_value['const_name'] . ' = \'' . $enum_value['value'] . '\';' . PHP_EOL; + } + } +} +?> + /** * {@inheritdoc} */ @@ -99,4 +116,59 @@ public static function find() return new (get_called_class()); } + + + $column_data){ + ?> + + /** + * column ENUM value labels + * @return array + */ + public static function () + { + return [ + $value){ + if ($generator->enableI18N) { + echo ' '.'self::' . $value['const_name'] . ' => Yii::t(\'' . $generator->messageCategory . '\', \'' . $value['value'] . "'),\n"; + } else { + echo ' '.'self::' . $value['const_name'] . ' => \'' . $value['value'] . "',\n"; + } + } + ?> + ]; + } + $column_data){ +?> + + /** + * @return string + */ + public function Label() + { + return self::()[$this->]??$this->; + } + + + /** + * @return bool + */ + public function () + { + return $this-> === self::; + } + + } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index a9b8059a4..0d8fb1133 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -8,10 +8,10 @@ $_SERVER['SCRIPT_NAME'] = '/' . __DIR__; $_SERVER['SCRIPT_FILENAME'] = __FILE__; -require_once(__DIR__ . '/../vendor/autoload.php'); -require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); +require_once(__DIR__ . '/../../../autoload.php'); +require_once(__DIR__ . '/../../../yiisoft/yii2/Yii.php'); Yii::setAlias('@yiiunit/gii', __DIR__); Yii::setAlias('@yii/gii', dirname(__DIR__) . '/src'); -require_once(__DIR__ . '/compatibility.php'); \ No newline at end of file +require_once(__DIR__ . '/compatibility.php'); diff --git a/tests/generators/ModelGeneratorTest.php b/tests/generators/ModelGeneratorTest.php index 3b70c09ff..959f7a8cb 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -413,4 +413,27 @@ public function testGenerateProperties($tableName, $columns) } } + + /** + * @dataProvider tablePropertiesProvider + * + */ + public function testEnum() + { + $generator = new ModelGenerator(); + $generator->template = 'default'; + $generator->tableName = 'category_photo'; + + $files = $generator->generate(); + +// $code = $files[0]->content; +// foreach ($columns as $column) { +// $location = strpos($code, $column['propertyRow']); +// $this->assertTrue( +// $location !== false, +// "Column \"{$column['columnName']}\" properties should be there:\n" . $column['propertyRow'] +// ); +// } + + } } From 31b3de33069a957ade776e4a8db1f18ef1e71390 Mon Sep 17 00:00:00 2001 From: Uldis Nelsons Date: Sun, 24 May 2020 23:20:28 +0300 Subject: [PATCH 02/19] Update bootstrap.php --- tests/bootstrap.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 0d8fb1133..655474143 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -8,8 +8,8 @@ $_SERVER['SCRIPT_NAME'] = '/' . __DIR__; $_SERVER['SCRIPT_FILENAME'] = __FILE__; -require_once(__DIR__ . '/../../../autoload.php'); -require_once(__DIR__ . '/../../../yiisoft/yii2/Yii.php'); +require_once(__DIR__ . '/../vendor/autoload.php'); +require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); Yii::setAlias('@yiiunit/gii', __DIR__); Yii::setAlias('@yii/gii', dirname(__DIR__) . '/src'); From b2bc2f33a94176bc3ec8b4f8d84eb5dea4fc5f6e Mon Sep 17 00:00:00 2001 From: uldisn Date: Mon, 25 May 2020 07:28:03 +0300 Subject: [PATCH 03/19] Fixed diplaying enum value --- src/generators/model/Generator.php | 4 ++-- src/generators/model/default/model.php | 4 ++-- tests/bootstrap.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 5d136feee..c6979dd62 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -503,7 +503,7 @@ private function generateManyManyRelations($table, $fks, $relations) $db = $this->getDbConnection(); foreach ($fks as $pair) { - [$firstKey, $secondKey] = $pair; + list($firstKey, $secondKey) = $pair; $table0 = $firstKey[0][0]; $table1 = $secondKey[0][0]; unset($firstKey[0][0], $secondKey[0][0]); @@ -1127,7 +1127,7 @@ protected function getEnum($columns) $enum[$column->name]['func_opts_name'] = 'opts'.$column_camel_name; $enum[$column->name]['func_get_label_name'] = 'get'.$column_camel_name.'ValueLabel'; $enum[$column->name]['isFunctionPrefix'] = 'is'.$column_camel_name; - $enum[$column->name]['getFunctionPrefix'] = 'get'.$column_camel_name; + $enum[$column->name]['displayFunctionPrefix'] = 'display'.$column_camel_name; $enum[$column->name]['columnName'] = $column->name; $enum[$column->name]['values'] = []; diff --git a/src/generators/model/default/model.php b/src/generators/model/default/model.php index 8abb85e80..c88196d07 100644 --- a/src/generators/model/default/model.php +++ b/src/generators/model/default/model.php @@ -150,9 +150,9 @@ public static function () /** * @return string */ - public function Label() + public function () { - return self::()[$this->]??$this->; + return self::()[$this->]; } Date: Sat, 26 Dec 2020 18:46:26 +0200 Subject: [PATCH 04/19] Added enum testing --- src/generators/model/Generator.php | 2 +- tests/generators/ModelGeneratorTest.php | 108 +++++++++++++++++++++--- 2 files changed, 96 insertions(+), 14 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index c6979dd62..35d144680 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -1115,7 +1115,7 @@ protected function isColumnAutoIncremental($table, $columns) * * @return array */ - protected function getEnum($columns) + public function getEnum($columns) { $enum = []; foreach ($columns as $column) { diff --git a/tests/generators/ModelGeneratorTest.php b/tests/generators/ModelGeneratorTest.php index 959f7a8cb..8982672f9 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -1,6 +1,8 @@ template = 'default'; $generator->tableName = 'category_photo'; - $files = $generator->generate(); -// $code = $files[0]->content; -// foreach ($columns as $column) { -// $location = strpos($code, $column['propertyRow']); -// $this->assertTrue( -// $location !== false, -// "Column \"{$column['columnName']}\" properties should be there:\n" . $column['propertyRow'] -// ); -// } + $tableSchema = $this->createEnumTableSchema(); + $params = [ + 'tableName' => $tableSchema->name, + 'className' => 'TestEnumModel', + 'queryClassName' => false, + 'tableSchema' => $tableSchema, + 'properties' => [], + 'labels' => $generator->generateLabels($tableSchema), + 'rules' => $generator->generateRules($tableSchema), + 'relations' => [], + 'relationsClassHints' => [], + 'enum' => $generator->getEnum($tableSchema->columns), + ]; + $codeFile = $generator->render('model.php', $params); + + /** + * Fix class code for eval - remove ?php, namespace and use Yii + */ + $classCode = str_replace('createEnumTableSchema(); + $testEnumModel->type = \TestEnumModel::TYPE_CLIENT; + $this->assertTrue($testEnumModel->isTypeClient()); + $this->assertFalse($testEnumModel->isTypeConsignees()); + + $testEnumModel->type = \TestEnumModel::TYPE_CONSIGNEES; + $this->assertFalse($testEnumModel->isTypeClient()); + $this->assertTrue($testEnumModel->isTypeConsignees()); + $this->assertEquals(\TestEnumModel::TYPE_CONSIGNEES,$testEnumModel->displayType()); + + + } + + public function createEnumTableSchema(): TableSchema + { + $schema = new TableSchema(); + $schema->name = 'company_type'; + $schema->fullName = 'company_type'; + $schema->primaryKey = ['id']; + $schema->columns = [ + 'id' => new ColumnSchema([ + 'name' => 'id', + 'allowNull' => false, + 'type' => 'smallint', + 'phpType' => 'integer', + 'dbType' => 'smallint(5) unsigned', + 'size' => 5, + 'precision' => 5, + 'isPrimaryKey' => true, + 'autoIncrement' => true, + 'unsigned' => true, + 'comment' => '' + ]), + 'type' => new ColumnSchema([ + 'name' => 'type', + 'allowNull' => true, + 'type' => 'string', + 'phpType' => 'string', + 'dbType' => 'enum(\'Client\',\'Consignees\',\'Car cleaner\')', + 'enumValues' => [ + 0 => 'Client', + 1 => 'Consignees', + 2 => 'Car cleaner', + ], + 'size' => null, + 'precision' => null, + 'isPrimaryKey' => false, + 'autoIncrement' => false, + 'unsigned' => false, + 'comment' => '' + ]), + ]; + return $schema; } } From 336c81aeb3c60ea04788326eec81ae96e8aca7d8 Mon Sep 17 00:00:00 2001 From: uldisn Date: Sat, 26 Dec 2020 18:49:02 +0200 Subject: [PATCH 05/19] Fixed test bug --- tests/generators/ModelGeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/generators/ModelGeneratorTest.php b/tests/generators/ModelGeneratorTest.php index 8982672f9..8557f1f42 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -476,7 +476,7 @@ public static function getTableSchema(){ } - public function createEnumTableSchema(): TableSchema + public function createEnumTableSchema() { $schema = new TableSchema(); $schema->name = 'company_type'; From d3d5b325b7c13bcb3c5d0426ce24d427edbd820d Mon Sep 17 00:00:00 2001 From: uldisn Date: Sat, 26 Dec 2020 18:59:59 +0200 Subject: [PATCH 06/19] In model test removed debugging --- tests/generators/ModelGeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/generators/ModelGeneratorTest.php b/tests/generators/ModelGeneratorTest.php index 8557f1f42..25d6cdc99 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -458,7 +458,7 @@ public static function getTableSchema(){ } '; if(!class_exists('TestEnumModel')) { - echo $classCode; + //echo $classCode; eval($classCode); } From d515600fcee8a557fe8b74fe52c5a0f8e77e0f37 Mon Sep 17 00:00:00 2001 From: uldisn Date: Sun, 27 Dec 2020 17:32:14 +0200 Subject: [PATCH 07/19] 1. Enum values get by ColumnSchema::$enumValues 2. To enum test added model->validation for rules testing --- src/generators/model/Generator.php | 4 +--- tests/generators/ModelGeneratorTest.php | 7 +++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 35d144680..45ef363dc 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -1131,9 +1131,7 @@ public function getEnum($columns) $enum[$column->name]['columnName'] = $column->name; $enum[$column->name]['values'] = []; - $enum_values = explode(',', substr($column->dbType, 4, strlen($column->dbType) - 1)); - - foreach ($enum_values as $value) { + foreach ($column->enumValues as $value) { $value = trim($value, "()'"); $const_name = strtoupper($column->name.'_'.$value); diff --git a/tests/generators/ModelGeneratorTest.php b/tests/generators/ModelGeneratorTest.php index 25d6cdc99..ec83d485d 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -464,6 +464,8 @@ public static function getTableSchema(){ $testEnumModel = new \TestEnumModel(); $testEnumModel::$testTableSchema = $this->createEnumTableSchema(); + + /** test assigning and method is... */ $testEnumModel->type = \TestEnumModel::TYPE_CLIENT; $this->assertTrue($testEnumModel->isTypeClient()); $this->assertFalse($testEnumModel->isTypeConsignees()); @@ -473,6 +475,11 @@ public static function getTableSchema(){ $this->assertTrue($testEnumModel->isTypeConsignees()); $this->assertEquals(\TestEnumModel::TYPE_CONSIGNEES,$testEnumModel->displayType()); + /** test validate */ + $this->assertTrue($testEnumModel->validate()); + $testEnumModel->type = '11111'; + $this->assertFalse($testEnumModel->validate()); + } From 414a02134fa59aa78bba547efc1336a8b137840f Mon Sep 17 00:00:00 2001 From: Uldis Nelsons Date: Tue, 30 Mar 2021 10:13:06 +0300 Subject: [PATCH 08/19] Update Generator.php --- src/generators/model/Generator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 45ef363dc..a3ffed85a 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -1161,6 +1161,6 @@ public function getEnum($columns) */ protected function isEnum($column) { - return stripos(strtoupper($column->dbType), 'ENUM') === 0; + return stripos($column->dbType, 'ENUM') !== false; } } From 7c1127640ffdd4f3fd6982227a920cc1f81c8945 Mon Sep 17 00:00:00 2001 From: Uldis Nelsons Date: Tue, 30 Mar 2021 10:21:23 +0300 Subject: [PATCH 09/19] Update Generator.php --- src/generators/model/Generator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index a3ffed85a..624959dbb 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -1161,6 +1161,6 @@ public function getEnum($columns) */ protected function isEnum($column) { - return stripos($column->dbType, 'ENUM') !== false; + return stripos($column->dbType, 'ENUM') === 0; } } From 39493aeebbea0e41907dca1df2c38ed64c6fc511 Mon Sep 17 00:00:00 2001 From: Uldis Nelsons Date: Wed, 31 Mar 2021 08:10:56 +0300 Subject: [PATCH 10/19] Update Generator.php --- src/generators/model/Generator.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 624959dbb..e3ea6170f 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -1156,11 +1156,11 @@ public function getEnum($columns) /** * validate is ENUM column. * - * @param ColumnSchema $column - * @return string + * @param ColumnSchema $column Column instance + * @return bool */ protected function isEnum($column) { - return stripos($column->dbType, 'ENUM') === 0; + return !empty($column->enumValues) || stripos($column->dbType, 'ENUM') === 0; } } From 8ef1697d65acbd7eea8143bec534a56d1160b565 Mon Sep 17 00:00:00 2001 From: uldisn Date: Thu, 1 Apr 2021 09:01:31 +0300 Subject: [PATCH 11/19] Done requested changes --- src/generators/model/Generator.php | 39 ++++++++--------- src/generators/model/default/model.php | 57 +++++++++++-------------- tests/generators/ModelGeneratorTest.php | 13 +++--- 3 files changed, 48 insertions(+), 61 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index e3ea6170f..f1937892d 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -431,15 +431,15 @@ public function generateRules($table) $rules[] = "[['" . implode("', '", $columns) . "'], 'string', 'max' => $length]"; } - foreach ($this->getEnum($table->columns) as $field_name => $field_details) { + foreach ($this->getEnum($table->columns) as $fieldName => $colummEnum) { $fieldEnumValues = []; - foreach ($field_details['values'] as $field_enum_values) { - $fieldEnumValues[] = 'self::'.$field_enum_values['const_name']; + foreach ($colummEnum['values'] as $fieldEnumValue) { + $fieldEnumValues[] = 'self::' . $fieldEnumValues['const_name']; } - $rules['enum-' . $field_name] = "['".$field_name."', 'in', 'range' => [\n ".implode( + $rules['enum-' . $fieldName] = "['" . $fieldName . "', 'in', 'range' => [\n " . implode( ",\n ", $fieldEnumValues - ).",\n ]\n ]"; + ) . ",\n ]\n ]"; } $db = $this->getDbConnection(); @@ -1109,7 +1109,7 @@ protected function isColumnAutoIncremental($table, $columns) } /** - * prepare ENUM field values. + * Prepares ENUM field values. * * @param ColumnSchema[] $columns * @@ -1123,29 +1123,24 @@ public function getEnum($columns) continue; } - $column_camel_name = str_replace(' ', '', ucwords(implode(' ', explode('_', $column->name)))); - $enum[$column->name]['func_opts_name'] = 'opts'.$column_camel_name; - $enum[$column->name]['func_get_label_name'] = 'get'.$column_camel_name.'ValueLabel'; - $enum[$column->name]['isFunctionPrefix'] = 'is'.$column_camel_name; - $enum[$column->name]['displayFunctionPrefix'] = 'display'.$column_camel_name; + $columnCamelName = Inflector::id2camel($column->name, '_'); + $enum[$column->name]['func_opts_name'] = 'opts' . $columnCamelName; + $enum[$column->name]['func_get_label_name'] = 'get' . $columnCamelName . 'ValueLabel'; + $enum[$column->name]['isFunctionPrefix'] = 'is' . $columnCamelName; + $enum[$column->name]['displayFunctionPrefix'] = 'display' . $columnCamelName; $enum[$column->name]['columnName'] = $column->name; $enum[$column->name]['values'] = []; foreach ($column->enumValues as $value) { - $value = trim($value, "()'"); - - $const_name = strtoupper($column->name.'_'.$value); - $const_name = preg_replace('/\s+/', '_', $const_name); - $const_name = str_replace(['-', '_', ' '], '_', $const_name); - $const_name = preg_replace('/[^A-Z0-9_]/', '', $const_name); + $constantName = Inflector::slug($column->name . ' ' . $value, '_'); $label = Inflector::camel2words($value); $enum[$column->name]['values'][] = [ 'value' => $value, - 'const_name' => $const_name, + 'const_name' => $constantName, 'label' => $label, - 'isFunctionSuffix' => str_replace(' ', '', ucwords(implode(' ', explode('_', $value)))) + 'isFunctionSuffix' => Inflector::camel2id('_', $value) ]; } } @@ -1154,13 +1149,13 @@ public function getEnum($columns) } /** - * validate is ENUM column. + * Checks if column is of ENUM type. * - * @param ColumnSchema $column Column instance + * @param ColumnSchema $column Column instance * @return bool */ protected function isEnum($column) { - return !empty($column->enumValues) || stripos($column->dbType, 'ENUM') === 0; + return !empty($column->enumValues) || stripos($column->dbType, 'ENUM') === 0; } } diff --git a/src/generators/model/default/model.php b/src/generators/model/default/model.php index c88196d07..78d786bf4 100644 --- a/src/generators/model/default/model.php +++ b/src/generators/model/default/model.php @@ -13,7 +13,7 @@ /* @var $labels string[] list of attribute labels (name => label) */ /* @var $rules string[] list of validation rules */ /* @var $relations array list of relations (name => relation declaration) */ -/* @var array $enum list of ENUM fields */ +/* @var $enum array list of ENUM fields */ echo " @@ -38,19 +38,17 @@ class extends baseClass, '\\') . "\n" ?> { - + /** - * ENUM field values - */ + * ENUM field values + */ $column_data){ - foreach ($column_data['values'] as $enum_value){ - echo ' const ' . $enum_value['const_name'] . ' = \'' . $enum_value['value'] . '\';' . PHP_EOL; + foreach($enum as $columnName => $columnData) { + foreach ($columnData['values'] as $enumValue){ + echo ' const ' . $enumValue['const_name'] . ' = \'' . $enumValue['value'] . '\';' . PHP_EOL; } } -} +endif ?> /** @@ -119,22 +117,22 @@ public static function find() $column_data){ +foreach ($enum as $columnName => $columnData) { ?> /** - * column ENUM value labels - * @return array - */ - public static function () + * column ENUM value labels + * @return array + */ + public static function () { return [ $value){ + foreach ($columnData['values'] as $k => $value) { if ($generator->enableI18N) { - echo ' '.'self::' . $value['const_name'] . ' => Yii::t(\'' . $generator->messageCategory . '\', \'' . $value['value'] . "'),\n"; + echo ' self::' . $value['const_name'] . ' => Yii::t(\'' . $generator->messageCategory . '\', \'' . $value['value'] . "'),\n"; } else { - echo ' '.'self::' . $value['const_name'] . ' => \'' . $value['value'] . "',\n"; + echo ' self::' . $value['const_name'] . ' => \'' . $value['value'] . "',\n"; } } ?> @@ -142,33 +140,30 @@ public static function () } $column_data){ + foreach ($enum as $columnName => $columnData) { ?> /** - * @return string - */ - public function () + * @return string + */ + public function () { - return self::()[$this->]; + return self::()[$this->]; } /** - * @return bool - */ - public function () + * @return bool + */ + public function () { - return $this-> === self::; + return $this-> === self::; } - } diff --git a/tests/generators/ModelGeneratorTest.php b/tests/generators/ModelGeneratorTest.php index ec83d485d..7d1b8860c 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -422,7 +422,6 @@ public function testEnum() $generator->template = 'default'; $generator->tableName = 'category_photo'; - $tableSchema = $this->createEnumTableSchema(); $params = [ 'tableName' => $tableSchema->name, @@ -441,12 +440,12 @@ public function testEnum() /** * Fix class code for eval - remove ?php, namespace and use Yii */ - $classCode = str_replace('type = '11111'; $this->assertFalse($testEnumModel->validate()); - } public function createEnumTableSchema() From 7beb53bd9ed5af8331dfcc729aacf6b1e086a40a Mon Sep 17 00:00:00 2001 From: uldisn Date: Thu, 1 Apr 2021 09:10:16 +0300 Subject: [PATCH 12/19] fixed bug --- src/generators/model/Generator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index f1937892d..0089ef13f 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -434,7 +434,7 @@ public function generateRules($table) foreach ($this->getEnum($table->columns) as $fieldName => $colummEnum) { $fieldEnumValues = []; foreach ($colummEnum['values'] as $fieldEnumValue) { - $fieldEnumValues[] = 'self::' . $fieldEnumValues['const_name']; + $fieldEnumValues[] = 'self::' . $fieldEnumValue['const_name']; } $rules['enum-' . $fieldName] = "['" . $fieldName . "', 'in', 'range' => [\n " . implode( ",\n ", From 3ab1c65899f66097d38c39942084c219a9a1335d Mon Sep 17 00:00:00 2001 From: uldisn Date: Thu, 1 Apr 2021 09:23:08 +0300 Subject: [PATCH 13/19] fixed bug --- src/generators/model/Generator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 0089ef13f..df44c58e8 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -1133,14 +1133,14 @@ public function getEnum($columns) foreach ($column->enumValues as $value) { - $constantName = Inflector::slug($column->name . ' ' . $value, '_'); + $constantName = strtoupper(Inflector::slug($column->name . ' ' . $value, '_')); $label = Inflector::camel2words($value); $enum[$column->name]['values'][] = [ 'value' => $value, 'const_name' => $constantName, 'label' => $label, - 'isFunctionSuffix' => Inflector::camel2id('_', $value) + 'isFunctionSuffix' => Inflector::id2camel( Inflector::slug( $value)) ]; } } From 4f9ab95f7e1d2ebd1d35123bb0819820c099edb7 Mon Sep 17 00:00:00 2001 From: uldisn Date: Thu, 1 Apr 2021 10:56:04 +0300 Subject: [PATCH 14/19] foreach(){} changed to foreach: endforeach; --- src/generators/model/default/model.php | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/generators/model/default/model.php b/src/generators/model/default/model.php index 78d786bf4..e508ea17b 100644 --- a/src/generators/model/default/model.php +++ b/src/generators/model/default/model.php @@ -116,9 +116,7 @@ public static function find() - $columnData) { - ?> + $columnData): ?> /** * column ENUM value labels @@ -127,21 +125,19 @@ public static function find() public static function () { return [ + $value): ?> $value) { if ($generator->enableI18N) { echo ' self::' . $value['const_name'] . ' => Yii::t(\'' . $generator->messageCategory . '\', \'' . $value['value'] . "'),\n"; } else { echo ' self::' . $value['const_name'] . ' => \'' . $value['value'] . "',\n"; } - } ?> + ]; } - $columnData) { -?> + + $columnData):?> /** * @return string @@ -150,9 +146,7 @@ public function () { return self::()[$this->]; } - + /** * @return bool @@ -161,9 +155,7 @@ public function === self::; } - + + + } From ac239cf041535eabfd0d3a8dfa6f442f414ea332 Mon Sep 17 00:00:00 2001 From: uldisn Date: Mon, 5 Apr 2021 15:29:57 +0300 Subject: [PATCH 15/19] 1. array keys changed to pascalCas 2. enum rule optimised 3. added for enums to model methods set[EnumField[To[EnumValue] --- src/generators/model/Generator.php | 17 +++++------------ src/generators/model/default/model.php | 21 +++++++++++++-------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index df44c58e8..6cd277856 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -432,14 +432,7 @@ public function generateRules($table) } foreach ($this->getEnum($table->columns) as $fieldName => $colummEnum) { - $fieldEnumValues = []; - foreach ($colummEnum['values'] as $fieldEnumValue) { - $fieldEnumValues[] = 'self::' . $fieldEnumValue['const_name']; - } - $rules['enum-' . $fieldName] = "['" . $fieldName . "', 'in', 'range' => [\n " . implode( - ",\n ", - $fieldEnumValues - ) . ",\n ]\n ]"; + $rules['enum-' . $fieldName] = "['" . $fieldName . "', 'in', 'range' => array_keys(self::".$colummEnum['funcOptsName']."())]"; } $db = $this->getDbConnection(); @@ -1124,9 +1117,9 @@ public function getEnum($columns) } $columnCamelName = Inflector::id2camel($column->name, '_'); - $enum[$column->name]['func_opts_name'] = 'opts' . $columnCamelName; - $enum[$column->name]['func_get_label_name'] = 'get' . $columnCamelName . 'ValueLabel'; + $enum[$column->name]['funcOptsName'] = 'opts' . $columnCamelName; $enum[$column->name]['isFunctionPrefix'] = 'is' . $columnCamelName; + $enum[$column->name]['setFunctionPrefix'] = 'set' . $columnCamelName . 'To'; $enum[$column->name]['displayFunctionPrefix'] = 'display' . $columnCamelName; $enum[$column->name]['columnName'] = $column->name; $enum[$column->name]['values'] = []; @@ -1138,9 +1131,9 @@ public function getEnum($columns) $enum[$column->name]['values'][] = [ 'value' => $value, - 'const_name' => $constantName, + 'constName' => $constantName, 'label' => $label, - 'isFunctionSuffix' => Inflector::id2camel( Inflector::slug( $value)) + 'functionSuffix' => Inflector::id2camel( Inflector::slug( $value)) ]; } } diff --git a/src/generators/model/default/model.php b/src/generators/model/default/model.php index e508ea17b..f6b69d44d 100644 --- a/src/generators/model/default/model.php +++ b/src/generators/model/default/model.php @@ -45,7 +45,7 @@ class extends baseClass, '\\') . $columnData) { foreach ($columnData['values'] as $enumValue){ - echo ' const ' . $enumValue['const_name'] . ' = \'' . $enumValue['value'] . '\';' . PHP_EOL; + echo ' const ' . $enumValue['constName'] . ' = \'' . $enumValue['value'] . '\';' . PHP_EOL; } } endif @@ -120,17 +120,17 @@ public static function find() /** * column ENUM value labels - * @return array + * @return string[] */ - public static function () + public static function () { return [ $value): ?> enableI18N) { - echo ' self::' . $value['const_name'] . ' => Yii::t(\'' . $generator->messageCategory . '\', \'' . $value['value'] . "'),\n"; + echo ' self::' . $value['constName'] . ' => Yii::t(\'' . $generator->messageCategory . '\', \'' . $value['value'] . "'),\n"; } else { - echo ' self::' . $value['const_name'] . ' => \'' . $value['value'] . "',\n"; + echo ' self::' . $value['constName'] . ' => \'' . $value['value'] . "',\n"; } ?> @@ -144,16 +144,21 @@ public static function () */ public function () { - return self::()[$this->]; + return self::()[$this->]; } /** * @return bool */ - public function () + public function () { - return $this-> === self::; + return $this-> === self::; + } + + public function () + { + $this-> = self::; } From 1b604ff5cd7adbb66949d2adfa187f91c66fdf0f Mon Sep 17 00:00:00 2001 From: uldisn Date: Tue, 6 Apr 2021 12:20:19 +0300 Subject: [PATCH 16/19] Code formatting --- src/generators/model/Generator.php | 7 ++++--- src/generators/model/default/model.php | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 6cd277856..7cb2fc59d 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -431,8 +431,9 @@ public function generateRules($table) $rules[] = "[['" . implode("', '", $columns) . "'], 'string', 'max' => $length]"; } - foreach ($this->getEnum($table->columns) as $fieldName => $colummEnum) { - $rules['enum-' . $fieldName] = "['" . $fieldName . "', 'in', 'range' => array_keys(self::".$colummEnum['funcOptsName']."())]"; + $columnsEnum = $this->getEnum($table->columns); + foreach ($columnsEnum as $fieldName => $colummEnum) { + $rules['enum-' . $fieldName] = "['" . $fieldName . "', 'in', 'range' => array_keys(self::" . $colummEnum['funcOptsName'] . '())]'; } $db = $this->getDbConnection(); @@ -1133,7 +1134,7 @@ public function getEnum($columns) 'value' => $value, 'constName' => $constantName, 'label' => $label, - 'functionSuffix' => Inflector::id2camel( Inflector::slug( $value)) + 'functionSuffix' => Inflector::id2camel(Inflector::slug($value)) ]; } } diff --git a/src/generators/model/default/model.php b/src/generators/model/default/model.php index f6b69d44d..d9859cb42 100644 --- a/src/generators/model/default/model.php +++ b/src/generators/model/default/model.php @@ -38,7 +38,7 @@ class extends baseClass, '\\') . "\n" ?> { - + /** * ENUM field values */ @@ -137,7 +137,7 @@ public static function () ]; } - $columnData):?> + $columnData): ?> /** * @return string From 78c8eed3a8d3326a377ee12bd1bb827ef970c591 Mon Sep 17 00:00:00 2001 From: uldisn Date: Tue, 6 Apr 2021 12:38:36 +0300 Subject: [PATCH 17/19] Code formatting --- src/generators/model/Generator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 7cb2fc59d..e3fd90f5b 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -432,8 +432,8 @@ public function generateRules($table) } $columnsEnum = $this->getEnum($table->columns); - foreach ($columnsEnum as $fieldName => $colummEnum) { - $rules['enum-' . $fieldName] = "['" . $fieldName . "', 'in', 'range' => array_keys(self::" . $colummEnum['funcOptsName'] . '())]'; + foreach ($columnsEnum as $fieldName => $columnEnum) { + $rules['enum-' . $fieldName] = "['" . $fieldName . "', 'in', 'range' => array_keys(self::" . $columnEnum['funcOptsName'] . '())]'; } $db = $this->getDbConnection(); From 16ac7d6b8b0deaa356e49e581de5a954e531b600 Mon Sep 17 00:00:00 2001 From: uldisn Date: Tue, 31 Oct 2023 21:14:57 +0200 Subject: [PATCH 18/19] Fixed merging bug --- src/generators/model/Generator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 1085b6b6b..4c7d170ef 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -1242,6 +1242,7 @@ protected function isEnum($column) return !empty($column->enumValues) || stripos($column->dbType, 'ENUM') === 0; } + /** * Returns the class name resolution * @param string $class * @return string From 9815f7b8c1233af746266bef2fb53da4e4dc77b1 Mon Sep 17 00:00:00 2001 From: uldisn Date: Mon, 29 Jan 2024 20:39:57 +0200 Subject: [PATCH 19/19] Added line in CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a8b8e0da..d2922ef42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Yii Framework 2 gii extension Change Log - Bug #532: Return `ExitCode::USAGE` on command input validation error (egmsystems) - Enh #537: Generating rules for the fields with default values (manky) - Enh #542: Use the table name to create the relation (thiagotalma) +- Enh #534: Generating in model ENUM fields value constants, setter and getter methods (uldisn) 2.2.6 May 22, 2023