1.0.7 doesn't generate 'required' rules

I`m just now testing Yii 1.0.7 version. I am using MySQL database.

I generated my model from this table:

CREATE TABLE treinamento.material (

mat_codigo int(10) unsigned NOT NULL auto_increment,

mat_ean char(13) NOT NULL,

mat_nome varchar(80) NOT NULL,

mat_preco decimal(13,2) unsigned NOT NULL,

mat_qtde decimal(13,2) unsigned NOT NULL default ‘0.00’,

teste varchar(45) NOT NULL,

PRIMARY KEY (mat_codigo)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Well, when I run ‘model’ command, my model does not come with the ‘required’ rules for my NOT NULL fields as before.

Seeing ModelCommand.php, I realized one change at line 387:




if(!$column->allowNull && $column->defaultValue===null)

	$required[]=$column->name;



In 1.0.6, it was




if(!$column->allowNull && !$column->defaultValue)

	$required[]=$column->name;



Well, at least in my mysql 5.0, I can’t have NOT NULL fields with DEFAULT NULL clause. MySQL browser and Navicat generate NOT NULL fields with empty string as default value.

I suggest the following change

In CDbColumnSchema, function typecast, change the order of the sentences


public function typecast($value)

	{		

		if($value==='')			

			return null;

			 

		if(gettype($value)===$this->type || $value===null || $value instanceof CDbExpression){			

			return $value;

		}

Empty strings are null, no matter what it means.

Null is different from empty string for both PHP and database.

I didn’t find the change to $column->defaultValue===null

I have found it in the actual release (1.0.7 r1212)

Ok, my fault. But, MySQL defines a empty string as default value for NOT NULL fields.

Does it happens only with me? It is still happening on 1.0.8 version (no one ‘required’ rule generated by model command)

My table in mySQl:




CREATE TABLE `teste` (

  `teste_id` int(11) NOT NULL auto_increment,

  `teste_nome` varchar(80) NOT NULL,

  `teste_uf` varchar(3) default NULL,

  PRIMARY KEY  (`teste_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

It was supposed to be a line like this


array('teste_nome','required'),

on my model. But, don’t.

I couldn’t reproduce the issue. If you execute “SHOW COLUMNS FROM teste”, what did you see?

Regarding the "if(!$column->allowNull && $column->defaultValue===null)", what I meant is that this line has been such since version 1.0.3.




Field           Type            Null    Key        Default    Extra

teste_id	int(11)	        NO	PRI	   (Null)     auto_increment

teste_nome	varchar(80)	NO			

teste_uf	varchar(3)	YES		   (Null)



*ps: As I said, MySQL doesn’t allow to define NULL default in NOT NULL fields. Not in my MySQL, at least :confused: