-
-
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?
Conversation
Signed-off-by: uldisn <uldisnelsons@gmail.com>
Signed-off-by: uldisn <uldisnelsons@gmail.com>
Signed-off-by: uldisn <uldisnelsons@gmail.com>
Signed-off-by: uldisn <uldisnelsons@gmail.com>
Fix yiisoft#416: Improved generation of model attributes and type annotations
…sDefaultValues
Rules default values
Signed-off-by: uldisn <uldisnelsons@gmail.com>
Signed-off-by: uldisn <uldisnelsons@gmail.com>
Signed-off-by: uldisn <uldisnelsons@gmail.com>
Thank you for putting effort in the improvement of the Yii framework. Unfortunately a use case is missing. It is required to get a better understanding of the pull request and helps us to determine the necessity and applicability of the suggested change to the framework. Could you supply us with a use case please? Please be as detailed as possible and show some code! Thanks! This is an automated comment, triggered by adding the label |
I think this should either be enabled by a parameter or done in a 2.2.x release. |
Realy is bug! See description. |
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.
👍
…NT_TIMESTAMP Signed-off-by: uldisn <uldisnelsons@gmail.com>
I don't get it from the description, but apparently @samdark does, so it's fine 😄 |
Signed-off-by: uldisn <uldisnelsons@gmail.com>
Thinking more about it... @uldisn what's the use case for having explicit |
Sometimes rules is fired, if not set default values. For example, if field required ( not null), in database is defined default value and in model not set. Then rule required fired. |
Isn't it better not to generate |
But my view is mirroring full table requirements in model. That allow in validation rules trust on default values. Currently my know problem is with require, but other problems can occur. For tuning masters yes. They can do it ( not to generate required rule in this case). Additionally i see follow improvements:
|
Any examples?
https://www.yiiframework.com/doc/api/2.0/yii-db-activerecord#loadDefaultValues()-detail is already there and works without any extra rules. |
On issue #335 is problem |
#335 seems to have another pull request https://github.com/yiisoft/yii2-gii/pull/421/files |
Solutions:
|
Signed-off-by: uldisn <uldisnelsons@gmail.com>
This reverts commit c83843f Signed-off-by: uldisn <uldisnelsons@gmail.com>
Generation default value rules cover follow problems:
Only need adapt for PostgrSQL |
that's magic in yii confuse me - why not define properties with defaults from db table? class Contact extends ActiveRecord
{
/**
* @var int
*/
public $id;
/**
* @var string Number
*/
public $contract_number = 'NoNumber';
...
} /**
* @var {type} {comment}
*/
public ${column} ={default}; |
src/generators/model/Generator.php
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
mssql/sql server:
DEFAULT definitions cannot be created on columns with a timestamp data type or columns with an IDENTITY property.
also it must be $defaultValue = 'time()';
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.
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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
@uldisn yes, my fail
if (strtoupper($column->defaultValue) === 'CURRENT_TIMESTAMP') {
if ($driverName === 'mysql' && $column->type === Schema::TYPE_TIMESTAMP) {
$defaultValue = 'time()';
} else {
$defaultValue = "date('Y-m-d H:i:s')";
}
}
For example, if default value is timestamp, it cannot set to property. |
@uldisn why? timestamp is integer |
$rules = []; | ||
$driverName = $this->getDbDriverName(); | ||
|
||
if (in_array($driverName, ['mysql', 'sqlite'], true)) { |
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:
/**
* @var string abstract type of this column. Possible abstract types include:
* char, string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
* timestamp, time, date, binary, and money.
*/
public $type;
/**
* @var string the PHP type of this column. Possible PHP types include:
* `string`, `boolean`, `integer`, `double`, `array`.
*/
public $phpType;
as you can see, $phpType
use only 5 types, you can detect numeric as if (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
src/generators/model/Generator.php
Outdated
|
||
foreach ($table->columns as $column) { | ||
|
||
if ($column->defaultValue !== 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.
if ($column->defaultValue === null && !$column->allowNull) { continue; }
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.
Not need "&& !$column->allowNull"
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.
ok
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 no need $columnsDefaultNull
set if ($column->allowNull) {$defaultValue = '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.
by default all attributes has null value. Not need.
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 null !== 'null'
, echo null
or echo false
don't print null
or false
, in my packages I use <?=json_encode($var)?>
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 checked MySql schema. If in MySql field no default value, then $column->defaultValue is null.
$column->allowNull use for rules "required"
Correct:
if ($column->defaultValue === null) { continue; }
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 $column->allowNull
is @param type|null $foo
, function setFoo(?type $value)
and function getFoo(): ?type
.
If in MySql field no default value, then $column->defaultValue is null.
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 ('...');
Bug
rules
If in rules no defined default value for field contract_number and not initiated, validation fired on require rule.
Example of generated rules: