-
-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Model rules for default values #420
base: master
Are you sure you want to change the base?
Changes from 15 commits
a13b973
f9adb4c
10f8d33
2b69aa0
e0a20b9
f4cb847
6676dae
32f9c95
05b0b09
1e4d743
57f2d72
73ff305
23b4a3a
e534105
0399a42
7f51d73
c83843f
a648b93
5cda9d9
cb3d572
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -335,6 +335,54 @@ public function generateRules($table) | |
{ | ||
$types = []; | ||
$lengths = []; | ||
$rules = []; | ||
$driverName = $this->getDbDriverName(); | ||
|
||
$columnsDefaultNull = []; | ||
$columnsDefaultValues = []; | ||
if (in_array($driverName, ['mysql', 'sqlite'], true)) { | ||
foreach ($table->columns as $column) { | ||
|
||
if ($column->defaultValue !== null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not need "&& !$column->allowNull" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @uldisn no need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by default all attributes has null value. Not need. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @uldisn There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked MySql schema. If in MySql field no default value, then $column->defaultValue is null. if ($column->defaultValue === null) { continue; } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @uldisn
CREATE TABLE `demo` (`a` VARCHAR(255) NULL, `b` TEXT NULL);
INSERT INTO `demo` (`a`) VALUES (NULL); // (NULL, NULL) CREATE TABLE ` demo` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`a` TEXT NULL DEFAULT NULL,
`b` VARCHAR(50) NULL DEFAULT 'is default'
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB;
INSERT INTO ` demo` (`a`, `b`) VALUES ('...', NULL);
INSERT INTO ` demo` (`a`, `b`) VALUES (NULL, '...');
INSERT INTO `demo` (`b`) VALUES (NULL);
INSERT INTO `demo` (`a`) VALUES ('...'); |
||
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\')'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mssql/sql server:
also it must be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For timestamp column, if default value not set, fired on require rule. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (strtoupper($column->defaultValue) === 'CURRENT_TIMESTAMP') {
if ($driverName === 'mssql') {
$defaultValue = $this->generateString($column->defaultValue);
} else {
$defaultValue = 'time()';
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On MSSQL fired rule, who validate actual field value - time format. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @uldisn yes, my fail
|
||
}else{ | ||
$defaultValue = '\'' . addslashes($column->defaultValue) . '\''; | ||
} | ||
break; | ||
|
||
default: | ||
$defaultValue = '\'' . addslashes($column->defaultValue) . '\''; | ||
samdark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
$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 ($table->columns as $column) { | ||
if ($column->autoIncrement) { | ||
continue; | ||
|
@@ -373,8 +421,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]"; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why only that's drivers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know only MySql.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sure just about MySql
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@uldisn that's types normalized check DB subfolders, also:
as you can see,
$phpType
use only 5 types, you can detect numeric asif (in_array($column->phpType, ['integer', 'double', 'float'], true))
'float' added because php dont have type 'double', i think it's used to set validation rule