From a13b973da5a7f524192d212daddedb11ab2c566b Mon Sep 17 00:00:00 2001 From: uldisn Date: Mon, 11 Nov 2019 11:56:35 +0200 Subject: [PATCH 01/17] improved generation annotation of properties Signed-off-by: uldisn --- src/generators/model/Generator.php | 56 ++++++++++++------ tests/generators/ModelGeneratorTest.php | 78 +++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 17 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index d34e55d10..22818211f 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -7,6 +7,7 @@ namespace yii\gii\generators\model; +use ReflectionClass; use Yii; use yii\base\NotSupportedException; use yii\db\ActiveQuery; @@ -254,7 +255,7 @@ public function generate() /** * Generates the properties for the specified table. - * @param \yii\db\TableSchema $table the table schema + * @param TableSchema $table the table schema * @return array the generated properties (property => type) * @since 2.0.6 */ @@ -262,13 +263,34 @@ protected function generateProperties($table) { $properties = []; foreach ($table->columns as $column) { - $columnPhpType = $column->phpType; - if ($columnPhpType === 'integer') { - $type = 'int'; - } elseif ($columnPhpType === 'boolean') { - $type = 'bool'; - } else { - $type = $columnPhpType; + switch ($column->type) { + case Schema::TYPE_SMALLINT: + case Schema::TYPE_INTEGER: + case Schema::TYPE_BIGINT: + case Schema::TYPE_TINYINT: + $type = 'int'; + break; + case Schema::TYPE_BOOLEAN: + $type = 'bool'; + break; + case Schema::TYPE_FLOAT: + case Schema::TYPE_DOUBLE: + case Schema::TYPE_DECIMAL: + case Schema::TYPE_MONEY: + $type = 'float'; + break; + case Schema::TYPE_DATE: + case Schema::TYPE_TIME: + case Schema::TYPE_DATETIME: + case Schema::TYPE_TIMESTAMP: + case Schema::TYPE_JSON: + $type = 'string'; + break; + default: + $type = $column->phpType; + } + if($column->allowNull){ + $type .= '|null'; } $properties[$column->name] = [ 'type' => $type, @@ -282,7 +304,7 @@ protected function generateProperties($table) /** * Generates the attribute labels for the specified table. - * @param \yii\db\TableSchema $table the table schema + * @param TableSchema $table the table schema * @return array the generated attribute labels (name => label) */ public function generateLabels($table) @@ -307,7 +329,7 @@ public function generateLabels($table) /** * Generates validation rules for the specified table. - * @param \yii\db\TableSchema $table the table schema + * @param TableSchema $table the table schema * @return array the generated validation rules */ public function generateRules($table) @@ -411,7 +433,7 @@ public function generateRules($table) /** * Generates relations using a junction table by adding an extra viaTable(). - * @param \yii\db\TableSchema the table being checked + * @param TableSchema the table being checked * @param array $fks obtained from the checkJunctionTable() method * @param array $relations * @return array modified $relations @@ -644,7 +666,7 @@ protected function generateRelationLink($refs) /** * Checks if the given table is a junction table, that is it has at least one pair of unique foreign keys. - * @param \yii\db\TableSchema the table being checked + * @param TableSchema the table being checked * @return array|bool all unique foreign key pairs if the table is a junction table, * or false if the table is not a junction table. */ @@ -688,7 +710,7 @@ protected function checkJunctionTable($table) /** * Generate a relation name for the specified table and a base name. * @param array $relations the relations being generated currently. - * @param \yii\db\TableSchema $table the table schema + * @param TableSchema $table the table schema * @param string $key a base name that the relation name may be generated from * @param bool $multiple whether this is a has-many relation * @return string the relation name @@ -696,10 +718,10 @@ protected function checkJunctionTable($table) protected function generateRelationName($relations, $table, $key, $multiple) { static $baseModel; - /* @var $baseModel \yii\db\ActiveRecord */ + /* @var $baseModel ActiveRecord */ if ($baseModel === null) { $baseClass = $this->baseClass; - $baseClassReflector = new \ReflectionClass($baseClass); + $baseClassReflector = new ReflectionClass($baseClass); if ($baseClassReflector->isAbstract()) { $baseClassWrapper = 'namespace ' . __NAMESPACE__ . ';'. @@ -950,12 +972,12 @@ protected function getDbDriverName() { /** @var Connection $db */ $db = $this->getDbConnection(); - return $db instanceof \yii\db\Connection ? $db->driverName : null; + return $db instanceof Connection ? $db->driverName : null; } /** * Checks if any of the specified columns is auto incremental. - * @param \yii\db\TableSchema $table the table schema + * @param TableSchema $table the table schema * @param array $columns columns to check for autoIncrement property * @return bool whether any of the specified columns is auto incremental. */ diff --git a/tests/generators/ModelGeneratorTest.php b/tests/generators/ModelGeneratorTest.php index 190dcc3d2..abc5d53f9 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -305,4 +305,82 @@ public function testGenerateNotSingularizedClassNames() $this->assertEquals($expectedClassName, $generatedClassName); } } + + /** + * @return array + */ + public function tablePropertiesProvider() + { + return [ + [ + 'tableName' => 'category_photo', + 'columns' => [ + [ + 'columnName' => 'id', + 'propertyRow' => '* @property int $id', + ], + [ + 'columnName' => 'category_id', + 'propertyRow' => '* @property int $category_id', + ], + [ + 'columnName' => 'display_number', + 'propertyRow' => '* @property int $display_number', + ], + + ] + ], + [ + 'tableName' => 'product', + 'columns' => [ + [ + 'columnName' => 'id', + 'propertyRow' => '* @property int $id', + ], + [ + 'columnName' => 'category_id', + 'propertyRow' => '* @property int $supplier_id', + ], + [ + 'columnName' => 'category_language_code', + 'propertyRow' => '* @property string $category_language_code', + ], + [ + 'columnName' => 'category_id', + 'propertyRow' => '* @property int $category_id', + ], + [ + 'columnName' => 'internal_name', + 'propertyRow' => '* @property string|null $internal_name', + ], + + ] + ], + ]; + + } + + /** + * @dataProvider tablePropertiesProvider + * + * @param $tableName string + * @param $columns + */ + public function testGenerateProperties($tableName, $columns){ + $generator = new ModelGenerator(); + $generator->template = 'default'; + $generator->tableName = $tableName; + + $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 f9adb4c7f0e9b3abee6558d675cf0c49edbd0b1b Mon Sep 17 00:00:00 2001 From: uldisn Date: Mon, 11 Nov 2019 12:13:28 +0200 Subject: [PATCH 02/17] Rolled back qualifiers --- src/generators/model/Generator.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 22818211f..8f8f202d5 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -7,7 +7,6 @@ namespace yii\gii\generators\model; -use ReflectionClass; use Yii; use yii\base\NotSupportedException; use yii\db\ActiveQuery; @@ -255,7 +254,7 @@ public function generate() /** * Generates the properties for the specified table. - * @param TableSchema $table the table schema + * @param \yii\db\TableSchema $table the table schema * @return array the generated properties (property => type) * @since 2.0.6 */ @@ -304,7 +303,7 @@ protected function generateProperties($table) /** * Generates the attribute labels for the specified table. - * @param TableSchema $table the table schema + * @param \yii\db\TableSchema $table the table schema * @return array the generated attribute labels (name => label) */ public function generateLabels($table) @@ -329,7 +328,7 @@ public function generateLabels($table) /** * Generates validation rules for the specified table. - * @param TableSchema $table the table schema + * @param \yii\db\TableSchema $table the table schema * @return array the generated validation rules */ public function generateRules($table) @@ -433,7 +432,7 @@ public function generateRules($table) /** * Generates relations using a junction table by adding an extra viaTable(). - * @param TableSchema the table being checked + * @param \yii\db\TableSchema the table being checked * @param array $fks obtained from the checkJunctionTable() method * @param array $relations * @return array modified $relations @@ -666,7 +665,7 @@ protected function generateRelationLink($refs) /** * Checks if the given table is a junction table, that is it has at least one pair of unique foreign keys. - * @param TableSchema the table being checked + * @param \yii\db\TableSchema the table being checked * @return array|bool all unique foreign key pairs if the table is a junction table, * or false if the table is not a junction table. */ @@ -710,7 +709,7 @@ protected function checkJunctionTable($table) /** * Generate a relation name for the specified table and a base name. * @param array $relations the relations being generated currently. - * @param TableSchema $table the table schema + * @param \yii\db\TableSchema $table the table schema * @param string $key a base name that the relation name may be generated from * @param bool $multiple whether this is a has-many relation * @return string the relation name @@ -718,10 +717,10 @@ protected function checkJunctionTable($table) protected function generateRelationName($relations, $table, $key, $multiple) { static $baseModel; - /* @var $baseModel ActiveRecord */ + /* @var $baseModel \yii\db\ActiveRecord */ if ($baseModel === null) { $baseClass = $this->baseClass; - $baseClassReflector = new ReflectionClass($baseClass); + $baseClassReflector = new \ReflectionClass($baseClass); if ($baseClassReflector->isAbstract()) { $baseClassWrapper = 'namespace ' . __NAMESPACE__ . ';'. @@ -972,12 +971,12 @@ protected function getDbDriverName() { /** @var Connection $db */ $db = $this->getDbConnection(); - return $db instanceof Connection ? $db->driverName : null; + return $db instanceof \yii\db\Connection ? $db->driverName : null; } /** * Checks if any of the specified columns is auto incremental. - * @param TableSchema $table the table schema + * @param \yii\db\TableSchema $table the table schema * @param array $columns columns to check for autoIncrement property * @return bool whether any of the specified columns is auto incremental. */ From 10f8d33c5e81ab5a2a1af318301a8bd3d3db45bd Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Tue, 12 Nov 2019 21:21:37 +0300 Subject: [PATCH 03/17] 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 8f8f202d5..73392cf50 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -288,7 +288,7 @@ protected function generateProperties($table) default: $type = $column->phpType; } - if($column->allowNull){ + if ($column->allowNull){ $type .= '|null'; } $properties[$column->name] = [ From 2b69aa09deee83f67d2ed56ae180c087a76c96dc Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Tue, 12 Nov 2019 21:22:50 +0300 Subject: [PATCH 04/17] Update ModelGeneratorTest.php --- tests/generators/ModelGeneratorTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/generators/ModelGeneratorTest.php b/tests/generators/ModelGeneratorTest.php index abc5d53f9..8437077d5 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -363,10 +363,11 @@ public function tablePropertiesProvider() /** * @dataProvider tablePropertiesProvider * - * @param $tableName string - * @param $columns + * @param string $tableName + * @param array $columns */ - public function testGenerateProperties($tableName, $columns){ + public function testGenerateProperties($tableName, $columns) + { $generator = new ModelGenerator(); $generator->template = 'default'; $generator->tableName = $tableName; From e0a20b99224cc3b9e1a36c7137345e1419f9cbe2 Mon Sep 17 00:00:00 2001 From: uldisn Date: Wed, 13 Nov 2019 06:54:42 +0200 Subject: [PATCH 05/17] Added changes to changelog.md Signed-off-by: uldisn --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e53ebcfd7..a7d9953c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 gii extension Change Log 2.1.3 under development ----------------------- -- no changes in this release. +- Improved generation of model attributes Phpdoc annotations types 2.1.2 October 08, 2019 From f4cb847ee398c43c0e5d4a825eb82805701fd885 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 13 Nov 2019 13:49:06 +0300 Subject: [PATCH 06/17] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7d9953c8..f19c2fead 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 gii extension Change Log 2.1.3 under development ----------------------- -- Improved generation of model attributes Phpdoc annotations types +- Enh #416: Improved generation of model attributes and type annotations (uldisn) 2.1.2 October 08, 2019 From 6676dae8516594b93a3cbf66d557a2606b495d82 Mon Sep 17 00:00:00 2001 From: uldisn Date: Sat, 16 Nov 2019 19:47:48 +0200 Subject: [PATCH 07/17] To model generator added rules for default values Signed-off-by: uldisn --- src/generators/model/Generator.php | 41 +++++++++++++++------ tests/bootstrap.php | 6 +-- tests/generators/ModelGeneratorTest.php | 49 +++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 15 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 73392cf50..c4e8a9475 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -7,6 +7,7 @@ namespace yii\gii\generators\model; +use ReflectionClass; use Yii; use yii\base\NotSupportedException; use yii\db\ActiveQuery; @@ -254,7 +255,7 @@ public function generate() /** * Generates the properties for the specified table. - * @param \yii\db\TableSchema $table the table schema + * @param TableSchema $table the table schema * @return array the generated properties (property => type) * @since 2.0.6 */ @@ -303,7 +304,7 @@ protected function generateProperties($table) /** * Generates the attribute labels for the specified table. - * @param \yii\db\TableSchema $table the table schema + * @param TableSchema $table the table schema * @return array the generated attribute labels (name => label) */ public function generateLabels($table) @@ -328,13 +329,31 @@ public function generateLabels($table) /** * Generates validation rules for the specified table. - * @param \yii\db\TableSchema $table the table schema + * @param TableSchema $table the table schema * @return array the generated validation rules */ public function generateRules($table) { $types = []; $lengths = []; + + $rules = []; + $columnsDefaultNull = []; + $driverName = $this->getDbDriverName(); + foreach ($table->columns as $column) { + if (in_array($driverName, ['mysql','sqlite'], true)) { + if ($column->defaultValue !== null) { + $rules[] = "[['" . $column->name . "'], 'default', 'value' => $column->defaultValue]"; + }elseif($column->allowNull){ + $columnsDefaultNull[] = $column->name; + } + } + } + + if($columnsDefaultNull){ + $rules[] = "[['" . implode("', '", $columnsDefaultNull) . "'], 'default', 'value' => null]"; + } + foreach ($table->columns as $column) { if ($column->autoIncrement) { continue; @@ -373,8 +392,6 @@ public function generateRules($table) } } } - $rules = []; - $driverName = $this->getDbDriverName(); foreach ($types as $type => $columns) { if ($driverName === 'pgsql' && $type === 'integer') { $rules[] = "[['" . implode("', '", $columns) . "'], 'default', 'value' => null]"; @@ -432,7 +449,7 @@ public function generateRules($table) /** * Generates relations using a junction table by adding an extra viaTable(). - * @param \yii\db\TableSchema the table being checked + * @param TableSchema the table being checked * @param array $fks obtained from the checkJunctionTable() method * @param array $relations * @return array modified $relations @@ -665,7 +682,7 @@ protected function generateRelationLink($refs) /** * Checks if the given table is a junction table, that is it has at least one pair of unique foreign keys. - * @param \yii\db\TableSchema the table being checked + * @param TableSchema the table being checked * @return array|bool all unique foreign key pairs if the table is a junction table, * or false if the table is not a junction table. */ @@ -709,7 +726,7 @@ protected function checkJunctionTable($table) /** * Generate a relation name for the specified table and a base name. * @param array $relations the relations being generated currently. - * @param \yii\db\TableSchema $table the table schema + * @param TableSchema $table the table schema * @param string $key a base name that the relation name may be generated from * @param bool $multiple whether this is a has-many relation * @return string the relation name @@ -717,10 +734,10 @@ protected function checkJunctionTable($table) protected function generateRelationName($relations, $table, $key, $multiple) { static $baseModel; - /* @var $baseModel \yii\db\ActiveRecord */ + /* @var $baseModel ActiveRecord */ if ($baseModel === null) { $baseClass = $this->baseClass; - $baseClassReflector = new \ReflectionClass($baseClass); + $baseClassReflector = new ReflectionClass($baseClass); if ($baseClassReflector->isAbstract()) { $baseClassWrapper = 'namespace ' . __NAMESPACE__ . ';'. @@ -971,12 +988,12 @@ protected function getDbDriverName() { /** @var Connection $db */ $db = $this->getDbConnection(); - return $db instanceof \yii\db\Connection ? $db->driverName : null; + return $db instanceof Connection ? $db->driverName : null; } /** * Checks if any of the specified columns is auto incremental. - * @param \yii\db\TableSchema $table the table schema + * @param TableSchema $table the table schema * @param array $columns columns to check for autoIncrement property * @return bool whether any of the specified columns is auto incremental. */ diff --git a/tests/bootstrap.php b/tests/bootstrap.php index a9b8059a4..2e2463259 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( '../../autoload.php'); +require_once( '../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 8437077d5..e730ac2dd 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -384,4 +384,53 @@ public function testGenerateProperties($tableName, $columns) } } + + /** + * @return array + */ + public function rulesDefaultValuesProvider() + { + return [ + [ + 'tableName' => 'customer', + 'columns' => [ + [ + 'columnName' => 'name', + 'rule' => '[[\'status\'], \'default\', \'value\' => 0]', + ], + [ + 'columnName' => 'address', + 'rule' => '[[\'name\', \'address\', \'profile_id\'], \'default\', \'value\' => null]', + ], + ] + ], + ]; + + } + + /** + * @dataProvider rulesDefaultValuesProvider + * + * @param string $tableName + * @param array $columns + */ + public function testRulesDefaultValues($tableName, $columns) + { + $generator = new ModelGenerator(); + $generator->template = 'default'; + $generator->tableName = $tableName; + + $files = $generator->generate(); + + $code = $files[0]->content; + foreach ($columns as $column) { + $location = strpos($code, $column['rule']); + $this->assertTrue( + $location !== false, + "Column \"{$column['columnName']}\" rule should be there:\n" . $column['rule'] + ); + } + + } + } From 32f9c954a74de40edf24808e846a7f91aacc358d Mon Sep 17 00:00:00 2001 From: uldisn Date: Sat, 16 Nov 2019 19:52:37 +0200 Subject: [PATCH 08/17] Reformated code Signed-off-by: uldisn --- src/generators/model/Generator.php | 9 +++++---- tests/bootstrap.php | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index c4e8a9475..cb3fdea77 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -337,20 +337,21 @@ public function generateRules($table) $types = []; $lengths = []; - $rules = []; $columnsDefaultNull = []; $driverName = $this->getDbDriverName(); foreach ($table->columns as $column) { - if (in_array($driverName, ['mysql','sqlite'], true)) { + if (in_array($driverName, ['mysql', 'sqlite'], true)) { if ($column->defaultValue !== null) { $rules[] = "[['" . $column->name . "'], 'default', 'value' => $column->defaultValue]"; - }elseif($column->allowNull){ + } elseif ($column->allowNull) { $columnsDefaultNull[] = $column->name; } } } - if($columnsDefaultNull){ + $rules = []; + + if ($columnsDefaultNull) { $rules[] = "[['" . implode("', '", $columnsDefaultNull) . "'], 'default', 'value' => null]"; } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 2e2463259..655474143 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -8,8 +8,8 @@ $_SERVER['SCRIPT_NAME'] = '/' . __DIR__; $_SERVER['SCRIPT_FILENAME'] = __FILE__; -require_once( '../../autoload.php'); -require_once( '../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 73ff305f5b6580aa5f5a4a39ce7b8fc1b8aedb1c Mon Sep 17 00:00:00 2001 From: uldisn Date: Sat, 16 Nov 2019 20:15:47 +0200 Subject: [PATCH 09/17] Fixes of fixes Signed-off-by: uldisn --- src/generators/model/Generator.php | 3 +-- tests/generators/ModelGeneratorTest.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index cb3fdea77..428e3f450 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -336,6 +336,7 @@ public function generateRules($table) { $types = []; $lengths = []; + $rules = []; $columnsDefaultNull = []; $driverName = $this->getDbDriverName(); @@ -349,8 +350,6 @@ public function generateRules($table) } } - $rules = []; - if ($columnsDefaultNull) { $rules[] = "[['" . implode("', '", $columnsDefaultNull) . "'], 'default', 'value' => null]"; } diff --git a/tests/generators/ModelGeneratorTest.php b/tests/generators/ModelGeneratorTest.php index e730ac2dd..4d86f5254 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -427,7 +427,7 @@ public function testRulesDefaultValues($tableName, $columns) $location = strpos($code, $column['rule']); $this->assertTrue( $location !== false, - "Column \"{$column['columnName']}\" rule should be there:\n" . $column['rule'] + "Rule should be there:\n" . $column['rule'] ); } From 23b4a3a1718af3d54f90936655888080ebec6e87 Mon Sep 17 00:00:00 2001 From: uldisn Date: Sat, 16 Nov 2019 20:20:50 +0200 Subject: [PATCH 10/17] Fix/fix/fix Signed-off-by: uldisn --- src/generators/model/Generator.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 428e3f450..f2d60889d 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -7,7 +7,6 @@ namespace yii\gii\generators\model; -use ReflectionClass; use Yii; use yii\base\NotSupportedException; use yii\db\ActiveQuery; @@ -255,7 +254,7 @@ public function generate() /** * Generates the properties for the specified table. - * @param TableSchema $table the table schema + * @param \yii\db\TableSchema $table the table schema * @return array the generated properties (property => type) * @since 2.0.6 */ @@ -304,7 +303,7 @@ protected function generateProperties($table) /** * Generates the attribute labels for the specified table. - * @param TableSchema $table the table schema + * @param \yii\db\TableSchema $table the table schema * @return array the generated attribute labels (name => label) */ public function generateLabels($table) @@ -329,7 +328,7 @@ public function generateLabels($table) /** * Generates validation rules for the specified table. - * @param TableSchema $table the table schema + * @param \yii\db\TableSchema $table the table schema * @return array the generated validation rules */ public function generateRules($table) @@ -449,7 +448,7 @@ public function generateRules($table) /** * Generates relations using a junction table by adding an extra viaTable(). - * @param TableSchema the table being checked + * @param \yii\db\TableSchema the table being checked * @param array $fks obtained from the checkJunctionTable() method * @param array $relations * @return array modified $relations @@ -682,7 +681,7 @@ protected function generateRelationLink($refs) /** * Checks if the given table is a junction table, that is it has at least one pair of unique foreign keys. - * @param TableSchema the table being checked + * @param \yii\db\TableSchema the table being checked * @return array|bool all unique foreign key pairs if the table is a junction table, * or false if the table is not a junction table. */ @@ -726,7 +725,7 @@ protected function checkJunctionTable($table) /** * Generate a relation name for the specified table and a base name. * @param array $relations the relations being generated currently. - * @param TableSchema $table the table schema + * @param \yii\db\TableSchema $table the table schema * @param string $key a base name that the relation name may be generated from * @param bool $multiple whether this is a has-many relation * @return string the relation name @@ -734,10 +733,10 @@ protected function checkJunctionTable($table) protected function generateRelationName($relations, $table, $key, $multiple) { static $baseModel; - /* @var $baseModel ActiveRecord */ + /* @var $baseModel \yii\db\ActiveRecord */ if ($baseModel === null) { $baseClass = $this->baseClass; - $baseClassReflector = new ReflectionClass($baseClass); + $baseClassReflector = new \ReflectionClass($baseClass); if ($baseClassReflector->isAbstract()) { $baseClassWrapper = 'namespace ' . __NAMESPACE__ . ';'. @@ -988,12 +987,12 @@ protected function getDbDriverName() { /** @var Connection $db */ $db = $this->getDbConnection(); - return $db instanceof Connection ? $db->driverName : null; + return $db instanceof \yii\db\Connection ? $db->driverName : null; } /** * Checks if any of the specified columns is auto incremental. - * @param TableSchema $table the table schema + * @param \yii\db\TableSchema $table the table schema * @param array $columns columns to check for autoIncrement property * @return bool whether any of the specified columns is auto incremental. */ From e5341056344490e799c363300ad15da70853037d Mon Sep 17 00:00:00 2001 From: uldisn Date: Sun, 17 Nov 2019 07:57:05 +0200 Subject: [PATCH 11/17] Added quoting for text default values Signed-off-by: uldisn --- src/generators/model/Generator.php | 29 +++++++++++++++++++++++-- tests/data/sqlite.sql | 2 +- tests/generators/ModelGeneratorTest.php | 8 +++++-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index f2d60889d..dade2c34d 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -336,19 +336,44 @@ public function generateRules($table) $types = []; $lengths = []; $rules = []; + $driverName = $this->getDbDriverName(); + /** + * Default values + */ $columnsDefaultNull = []; - $driverName = $this->getDbDriverName(); + $columnsDefaultValues = []; foreach ($table->columns as $column) { if (in_array($driverName, ['mysql', 'sqlite'], true)) { + /** + * text default values quote + */ if ($column->defaultValue !== null) { - $rules[] = "[['" . $column->name . "'], 'default', 'value' => $column->defaultValue]"; + switch ($column->type) { + case Schema::TYPE_SMALLINT: + case Schema::TYPE_INTEGER: + case Schema::TYPE_BIGINT: + case Schema::TYPE_TINYINT: + case Schema::TYPE_BOOLEAN: + case Schema::TYPE_FLOAT: + case Schema::TYPE_DOUBLE: + case Schema::TYPE_DECIMAL: + case Schema::TYPE_MONEY: + $defaultValue = $column->defaultValue; + break; + default: + $defaultValue = '\'' . $column->defaultValue . '\''; + } + $columnsDefaultValues[$defaultValue][] = $column->name; } elseif ($column->allowNull) { $columnsDefaultNull[] = $column->name; } } } + foreach($columnsDefaultValues as $defaultValue => $columnNameList){ + $rules[] = "[['" . implode("', '", $columnNameList) . "'], 'default', 'value' => $defaultValue]"; + } if ($columnsDefaultNull) { $rules[] = "[['" . implode("', '", $columnsDefaultNull) . "'], 'default', 'value' => null]"; } diff --git a/tests/data/sqlite.sql b/tests/data/sqlite.sql index 619f1e9a5..3c11a9500 100644 --- a/tests/data/sqlite.sql +++ b/tests/data/sqlite.sql @@ -21,7 +21,7 @@ CREATE TABLE "customer" ( id INTEGER NOT NULL, email varchar(128) NOT NULL, name varchar(128), - address text, + address text DEFAULT '-', status INTEGER DEFAULT 0, profile_id INTEGER, PRIMARY KEY (id) diff --git a/tests/generators/ModelGeneratorTest.php b/tests/generators/ModelGeneratorTest.php index 4d86f5254..c17f4f8ef 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -400,7 +400,11 @@ public function rulesDefaultValuesProvider() ], [ 'columnName' => 'address', - 'rule' => '[[\'name\', \'address\', \'profile_id\'], \'default\', \'value\' => null]', + 'rule' => '[[\'name\', \'profile_id\'], \'default\', \'value\' => null]', + ], + [ + 'columnName' => 'address', + 'rule' => '[[\'address\'], \'default\', \'value\' => \'-\']', ], ] ], @@ -427,7 +431,7 @@ public function testRulesDefaultValues($tableName, $columns) $location = strpos($code, $column['rule']); $this->assertTrue( $location !== false, - "Rule should be there:\n" . $column['rule'] + "Column \"{$column['columnName']}\" rule should be there:\n" . $column['rule'] ); } From 0399a4203fbf97aa82dd6ae317f6f93c049419bc Mon Sep 17 00:00:00 2001 From: uldisn Date: Tue, 19 Nov 2019 11:27:28 +0200 Subject: [PATCH 12/17] Done requested changes and added special case for default value CURRENT_TIMESTAMP Signed-off-by: uldisn --- src/generators/model/Generator.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index dade2c34d..b856328f7 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -338,16 +338,11 @@ public function generateRules($table) $rules = []; $driverName = $this->getDbDriverName(); - /** - * Default values - */ $columnsDefaultNull = []; $columnsDefaultValues = []; - foreach ($table->columns as $column) { - if (in_array($driverName, ['mysql', 'sqlite'], true)) { - /** - * text default values quote - */ + if (in_array($driverName, ['mysql', 'sqlite'], true)) { + foreach ($table->columns as $column) { + if ($column->defaultValue !== null) { switch ($column->type) { case Schema::TYPE_SMALLINT: @@ -361,8 +356,18 @@ public function generateRules($table) case Schema::TYPE_MONEY: $defaultValue = $column->defaultValue; break; + + case Schema::TYPE_DATETIME: + case Schema::TYPE_TIMESTAMP: + if(strtoupper($column->defaultValue) === 'CURRENT_TIMESTAMP'){ + $defaultValue = 'date(\'Y-m-d H:i:s\')'; + }else{ + $defaultValue = '\'' . addslashes($column->defaultValue) . '\''; + } + break; + default: - $defaultValue = '\'' . $column->defaultValue . '\''; + $defaultValue = '\'' . addslashes($column->defaultValue) . '\''; } $columnsDefaultValues[$defaultValue][] = $column->name; } elseif ($column->allowNull) { From 7f51d733ff0a364366b303b751895cd4a140178b Mon Sep 17 00:00:00 2001 From: uldisn Date: Tue, 19 Nov 2019 12:04:03 +0200 Subject: [PATCH 13/17] For default value escaping used $db->getSchema()->quoteValue() Signed-off-by: uldisn --- src/generators/model/Generator.php | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index b856328f7..b3a90d89f 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -330,6 +330,7 @@ public function generateLabels($table) * Generates validation rules for the specified table. * @param \yii\db\TableSchema $table the table schema * @return array the generated validation rules + * @throws NotSupportedException */ public function generateRules($table) { @@ -338,9 +339,12 @@ public function generateRules($table) $rules = []; $driverName = $this->getDbDriverName(); - $columnsDefaultNull = []; - $columnsDefaultValues = []; if (in_array($driverName, ['mysql', 'sqlite'], true)) { + + $db = $this->getDbConnection(); + $columnsDefaultNull = []; + $columnsDefaultValues = []; + foreach ($table->columns as $column) { if ($column->defaultValue !== null) { @@ -362,26 +366,27 @@ public function generateRules($table) if(strtoupper($column->defaultValue) === 'CURRENT_TIMESTAMP'){ $defaultValue = 'date(\'Y-m-d H:i:s\')'; }else{ - $defaultValue = '\'' . addslashes($column->defaultValue) . '\''; + $defaultValue = $db->getSchema()->quoteValue($column->defaultValue); } break; default: - $defaultValue = '\'' . addslashes($column->defaultValue) . '\''; + $defaultValue = $db->getSchema()->quoteValue($column->defaultValue); } $columnsDefaultValues[$defaultValue][] = $column->name; } elseif ($column->allowNull) { $columnsDefaultNull[] = $column->name; } } + foreach($columnsDefaultValues as $defaultValue => $columnNameList){ + $rules[] = "[['" . implode("', '", $columnNameList) . "'], 'default', 'value' => $defaultValue]"; + } + if ($columnsDefaultNull) { + $rules[] = "[['" . implode("', '", $columnsDefaultNull) . "'], 'default', 'value' => null]"; + } } - foreach($columnsDefaultValues as $defaultValue => $columnNameList){ - $rules[] = "[['" . implode("', '", $columnNameList) . "'], 'default', 'value' => $defaultValue]"; - } - if ($columnsDefaultNull) { - $rules[] = "[['" . implode("', '", $columnsDefaultNull) . "'], 'default', 'value' => null]"; - } + foreach ($table->columns as $column) { if ($column->autoIncrement) { From c83843f6710f15af6ffc0534b9bf3c8477b79160 Mon Sep 17 00:00:00 2001 From: uldisn Date: Fri, 22 Nov 2019 13:00:30 +0200 Subject: [PATCH 14/17] Uz Yii 2.0.30 Signed-off-by: uldisn --- tests/bootstrap.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 655474143..3929a3404 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -8,8 +8,8 @@ $_SERVER['SCRIPT_NAME'] = '/' . __DIR__; $_SERVER['SCRIPT_FILENAME'] = __FILE__; -require_once(__DIR__ . '/../vendor/autoload.php'); -require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); +require_once('../../autoload.php'); +require_once('../yii2/Yii.php'); Yii::setAlias('@yiiunit/gii', __DIR__); Yii::setAlias('@yii/gii', dirname(__DIR__) . '/src'); From a648b93cb56afbf98e8311cb91b8db66d6046ba8 Mon Sep 17 00:00:00 2001 From: uldisn Date: Fri, 22 Nov 2019 13:02:42 +0200 Subject: [PATCH 15/17] Revert "Uz Yii 2.0.30" This reverts commit c83843f6 Signed-off-by: uldisn --- tests/bootstrap.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3929a3404..655474143 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -8,8 +8,8 @@ $_SERVER['SCRIPT_NAME'] = '/' . __DIR__; $_SERVER['SCRIPT_FILENAME'] = __FILE__; -require_once('../../autoload.php'); -require_once('../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 5cda9d970d265adae1996fb841572355b317457e Mon Sep 17 00:00:00 2001 From: Uldis Nelsons Date: Mon, 12 Apr 2021 23:04:45 +0300 Subject: [PATCH 16/17] Update Generator.php --- src/generators/model/Generator.php | 60 ++++++++++++++++-------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index b3a90d89f..82bd49e28 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -346,41 +346,45 @@ public function generateRules($table) $columnsDefaultValues = []; foreach ($table->columns as $column) { + + if ($column->defaultValue === null) { + if ($column->allowNull) { + $columnsDefaultNull[] = $column->name; + } + continue; + } + switch ($column->type) { + case Schema::TYPE_SMALLINT: + case Schema::TYPE_INTEGER: + case Schema::TYPE_BIGINT: + case Schema::TYPE_TINYINT: + case Schema::TYPE_BOOLEAN: + case Schema::TYPE_FLOAT: + case Schema::TYPE_DOUBLE: + case Schema::TYPE_DECIMAL: + case Schema::TYPE_MONEY: + $defaultValue = $column->defaultValue; + break; - if ($column->defaultValue !== null) { - switch ($column->type) { - case Schema::TYPE_SMALLINT: - case Schema::TYPE_INTEGER: - case Schema::TYPE_BIGINT: - case Schema::TYPE_TINYINT: - case Schema::TYPE_BOOLEAN: - case Schema::TYPE_FLOAT: - case Schema::TYPE_DOUBLE: - case Schema::TYPE_DECIMAL: - case Schema::TYPE_MONEY: - $defaultValue = $column->defaultValue; - break; - - case Schema::TYPE_DATETIME: - case Schema::TYPE_TIMESTAMP: - if(strtoupper($column->defaultValue) === 'CURRENT_TIMESTAMP'){ - $defaultValue = 'date(\'Y-m-d H:i:s\')'; - }else{ - $defaultValue = $db->getSchema()->quoteValue($column->defaultValue); - } - break; - - default: + case Schema::TYPE_DATETIME: + case Schema::TYPE_TIMESTAMP: + if(strtoupper($column->defaultValue) === 'CURRENT_TIMESTAMP'){ + $defaultValue = 'date(\'Y-m-d H:i:s\')'; + }else{ $defaultValue = $db->getSchema()->quoteValue($column->defaultValue); - } - $columnsDefaultValues[$defaultValue][] = $column->name; - } elseif ($column->allowNull) { - $columnsDefaultNull[] = $column->name; + } + break; + + default: + $defaultValue = $db->getSchema()->quoteValue($column->defaultValue); } + $columnsDefaultValues[$defaultValue][] = $column->name } + foreach($columnsDefaultValues as $defaultValue => $columnNameList){ $rules[] = "[['" . implode("', '", $columnNameList) . "'], 'default', 'value' => $defaultValue]"; } + if ($columnsDefaultNull) { $rules[] = "[['" . implode("', '", $columnsDefaultNull) . "'], 'default', 'value' => null]"; } From cb3d5722df665ca4e8a8b2edf88ae4cc2e50de8b Mon Sep 17 00:00:00 2001 From: Uldis Nelsons Date: Mon, 12 Apr 2021 23:10:09 +0300 Subject: [PATCH 17/17] Update Generator.php --- 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 82bd49e28..67ab77128 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -378,9 +378,9 @@ public function generateRules($table) default: $defaultValue = $db->getSchema()->quoteValue($column->defaultValue); } - $columnsDefaultValues[$defaultValue][] = $column->name + $columnsDefaultValues[$defaultValue][] = $column->name; } - + foreach($columnsDefaultValues as $defaultValue => $columnNameList){ $rules[] = "[['" . implode("', '", $columnNameList) . "'], 'default', 'value' => $defaultValue]"; }