Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

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

heyhoo opened this Issue Jan 19, 2013 · 5 comments


2 participants

heyhoo commented Jan 19, 2013


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.

@heyhoo heyhoo referenced this issue Jan 23, 2013


Issue #2014 #2029


resurtm commented Mar 15, 2013

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.

@resurtm resurtm was assigned Mar 15, 2013


heyhoo commented Mar 19, 2013

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 commented Apr 5, 2013

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.', ';
    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 commented Apr 23, 2013

@heyhoo, any news on this?


resurtm commented May 4, 2013

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.

@resurtm resurtm closed this May 4, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment