-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Closed
Milestone
Description
在表admin中,字段
`is_admin` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否是管理员,0不是,1是',
表单数据提交时,该字段没有输入任何值,提交过来时,is_admin 的值为 string(0) ""
在yii\db\ColumnSchema 中方法typecast如下:
/**
* Converts the input value according to [[phpType]].
* If the value is null or an [[Expression]], it will not be converted.
* @param mixed $value input value
* @return mixed converted value
*/
public function typecast($value)
{
if ($value === '' && $this->type !== Schema::TYPE_TEXT && $this->type !== Schema::TYPE_STRING && $this->type !== Schema::TYPE_BINARY) {
return null;
}
if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
return $value;
}
switch ($this->phpType) {
case 'string':
return is_resource($value) ? $value : (string) $value;
case 'integer':
return (integer) $value;
case 'boolean':
return (boolean) $value;
}
return $value;
}
该值在下面条件中,返回值为NULL
if ($value === '' && $this->type !== Schema::TYPE_TEXT && $this->type !== Schema::TYPE_STRING && $this->type !== Schema::TYPE_BINARY) {
return null;
}
这样的话,值为空字符,而且类型不是text,string,binary的字段返回的都是NULL(就算$this->allowNul==false也是这样)
如果表设计时该字段是NOT NULL,就会导致插入时报错。
而在yii1.x中,该方法是
public function typecast($value)
{
if(gettype($value)===$this->type || $value===null || $value instanceof CDbExpression)
return $value;
if($value==='' && $this->allowNull)
return $this->type==='string' ? '' : null;
switch($this->type)
{
case 'string': return (string)$value;
case 'integer': return (integer)$value;
case 'boolean': return (boolean)$value;
case 'double':
default: return $value;
}
}