yiisoft/yii — Issue #389: Separate precision from in CDbSchema::$columnTypes
Author: ksangers
Created: 2012-02-23 09:31:54+00:00
State: open

As seen on: http://www.yiiframework.com/forum/index.php/topic/29091-separate-precision-from-in-cdbschemacolumntypes/

I started working with Yii migrate scripts and noticed something. It doesn't support to change the default precision. I'm suggesting a change to CDbSchema::$columnTypes to an array without precision. For example in CMssqlScheme I would replace the following:

public $columnTypes=array(
...
'string' => 'varchar(255)',
...
);

with this code:

public $columnTypes=array(
...
'string' => 'varchar', // or nvarchar even, because mssql varchar doesn't store multibyte characters
...
);

This would be similar in CMysqlScheme and the other database-specific files. This requires some changes to CDbScheme::getColumnType(), something like:

public function getColumnType($type)
{
if(isset($this->columnTypes[$type]))
return $this->columnTypes[$type];
/* added the following 2 lines */
else if (preg_match_all('/([a-zA-Z]+)\(([0-9,\s]*)\)/',$type,$matches))
return $this->getColumnType($matches[1][0])."(".$matches[2][0].")";
else if(($pos=strpos($type,' '))!==false)
{
$t=substr($type,0,$pos);
return (isset($this->columnTypes[$t]) ? $this->columnTypes[$t] : $t).substr($type,$pos);
}
else
return $type;
}