diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 73392cf50..67ab77128 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -330,11 +330,68 @@ 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) { $types = []; $lengths = []; + $rules = []; + $driverName = $this->getDbDriverName(); + + if (in_array($driverName, ['mysql', 'sqlite'], true)) { + + $db = $this->getDbConnection(); + $columnsDefaultNull = []; + $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; + + 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: + $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]"; + } + } + + + foreach ($table->columns as $column) { if ($column->autoIncrement) { continue; @@ -373,8 +430,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]"; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index a9b8059a4..655474143 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -14,4 +14,4 @@ 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/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 8437077d5..c17f4f8ef 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -384,4 +384,57 @@ 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\', \'profile_id\'], \'default\', \'value\' => null]', + ], + [ + 'columnName' => 'address', + 'rule' => '[[\'address\'], \'default\', \'value\' => \'-\']', + ], + ] + ], + ]; + + } + + /** + * @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'] + ); + } + + } + }