Skip to content
This repository

Gii model generator does not generate rules for required fields #2014

Closed
heyhoo opened this Issue · 5 comments

2 participants

Mark van den Broek resurtm
Mark van den Broek

File:
../framework/gii/generators/model/ModelCode.php

Line: 223
$r=!$column->allowNull && $column->defaultValue===null;

This will always return false because you can't specify a column to be NOT NULL with a default value of NULL.
Because $r always returns false rules for required fields are not generated.

Mark van den Broek heyhoo referenced this issue from a commit in heyhoo/yii
Mark van den Broek heyhoo Bugfix: issue #2014 e368e5d
Mark van den Broek heyhoo referenced this issue
Closed

Issue #2014 #2029

resurtm
Collaborator

Could you please specify more details? Your testing database table, database type and version, etc.

I've tested following DDL (MySQL 5.5) which represents all combinations of NULL, DEFAULT 0 and DEFAULT NULL:

create table tbl_test_table
(
    `id` int(11) not null, /* becomes required */
    `c1` int(11) not null, /* becomes required */
    `c2` int(11) not null default 0, /* becomes not required */
    /*`c3` int(11) not null default null,*/ /* impossible, skipped */
    `c4` int(11) null, /* becomes not required, default is null */
    `c5` int(11) null default 0, /* becomes not required */
    `c6` int(11) null default null, /* becomes not required */
    primary key (`id`)
)
engine=InnoDB default charset=utf8;

Your patch (provided in #2029) produces exactly same result as the current code.

Mark van den Broek

I am using PHP Version 5.1.6, MySQL 5.0.22.

I have also tested with your test table and in my case both id and c1 do not become required.

Guess that this is a MySQL issue, the default value does not get NULL for id and c1.
So in that case $column->defaultValue===null will not result in true, which doesn't make these fields become required.

resurtm
Collaborator

I've tested this with 5.0.96-community and bug does not occur (results are same as before). Could you create table i've posted above and post here results of the following code:

foreach(Yii::app()->db->schema->getTable('tbl_test_table')->columns as $column)
{
    echo $column->name.', ';
    CVarDumper::dump($column->defaultValue);
    echo "\n";
}

My result:

id, null
c1, null
c2, 0
c4, null
c5, 0
c6, null

It's interesting what you're really getting.

resurtm
Collaborator

@heyhoo, any news on this?

resurtm resurtm closed this
resurtm
Collaborator
resurtm commented

I'm closing this since long time has passed without any chance to reproduce it. Please provide more details on how can i reproduce your problem and i'll reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.