yii2-gii
yii2-gii copied to clipboard
Model rules for default values
| Q | A |
|---|---|
| Is bugfix? | yes |
| New feature? | yes |
| Breaks BC? | no |
| Tests pass? | yes |
| Fixed issues | - |
Bug
CREATE TABLE `contract` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`contract_number` varchar(50) NOT NULL DEFAULT 'NoNumber' COMMENT 'Number',
`contract_date` date DEFAULT NULL COMMENT 'Contract date',
`contract_place` varchar(250) DEFAULT NULL COMMENT 'Contract place',
`notes` text COMMENT 'Notes',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
rules
/**
* @inheritdoc
*/
public function rules()
{
return [
[['contract_number'], 'required'],
[['contract_date'], 'safe'],
[['notes'], 'string'],
[['contract_number'], 'string', 'max' => 50],
[['contract_place'], 'string', 'max' => 250],
];
}
If in rules no defined default value for field contract_number and not initiated, validation fired on require rule.
Example of generated rules:
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name', 'address', 'profile_id'], 'default', 'value' => null],
[['status'], 'integer', 'default', 'value' => 0],
[['email'], 'required'],
[['address'], 'string'],
[['status', 'profile_id'], 'integer'],
[['email', 'name'], 'string', 'max' => 128],
];
Thank you for putting effort in the improvement of the Yii framework. We have reviewed your pull request.
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 pr:missing usecase.
I think this should either be enabled by a parameter or done in a 2.2.x release.
Realy is bug! See description.
I don't get it from the description, but apparently @samdark does, so it's fine :smile:
Thinking more about it... @uldisn what's the use case for having explicit default rules if they match database ones? As far as I know, database silently uses default when value is not provided anyway...
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. For optimal indexing not null is very actual, but usually using default CURRENT_TIMESTAMP fired on require rule.
Isn't it better not to generate required rule in this case?
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:
- default value define as model constant
- create method "loadDefaultValues()" - useful for new record before showing in form.
Currently my know problem is with require, but other problems can occur.
Any examples?
create method "loadDefaultValues()" - useful for new record before showing in form.
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:
- database fields with not null and default value not include in require rule
- fired only on like #335 issue described situation
- minimal and optimal code
- create full list rules for default values
- work correctly in any situation
- huge code
- create full list rules for default values excluding null default value
- work correctly in any situation
- no so huge code
Generation default value rules cover follow problems:
- Gii sets wrong model rule for integer field with default value #335 #421
- The generator makes all integer columns as […, 'default', 'value' => null] even column set as not nul #402
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};
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};
For example, if default value is timestamp, it cannot set to property.
@uldisn why? timestamp is integer