yii2-gii icon indicating copy to clipboard operation
yii2-gii copied to clipboard

Model rules for default values

Open uldisn opened this issue 6 years ago • 16 comments

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],
        ];
    

uldisn avatar Nov 16 '19 18:11 uldisn

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.

yii-bot avatar Nov 18 '19 11:11 yii-bot

I think this should either be enabled by a parameter or done in a 2.2.x release.

schmunk42 avatar Nov 18 '19 11:11 schmunk42

Realy is bug! See description.

uldisn avatar Nov 18 '19 17:11 uldisn

I don't get it from the description, but apparently @samdark does, so it's fine :smile:

schmunk42 avatar Nov 19 '19 09:11 schmunk42

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...

samdark avatar Nov 19 '19 10:11 samdark

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.

uldisn avatar Nov 19 '19 10:11 uldisn

Isn't it better not to generate required rule in this case?

samdark avatar Nov 19 '19 10:11 samdark

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.

uldisn avatar Nov 19 '19 11:11 uldisn

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.

samdark avatar Nov 19 '19 11:11 samdark

On issue #335 is problem

uldisn avatar Nov 20 '19 14:11 uldisn

#335 seems to have another pull request https://github.com/yiisoft/yii2-gii/pull/421/files

samdark avatar Nov 20 '19 14:11 samdark

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

uldisn avatar Nov 20 '19 19:11 uldisn

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

uldisn avatar Jan 17 '20 16:01 uldisn

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};

WinterSilence avatar Mar 08 '21 17:03 WinterSilence

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 avatar Apr 12 '21 06:04 uldisn

@uldisn why? timestamp is integer

WinterSilence avatar Apr 12 '21 08:04 WinterSilence