Exemplo n.º 1
0
 /**
  *
  * @param \yii\db\ColumnSchema $column
  * @return array
  */
 public function getSchemaType($column)
 {
     if ($this->constans === null) {
         $this->constans = [];
         $ref = new \ReflectionClass(Schema::className());
         foreach ($ref->getConstants() as $constName => $constValue) {
             if (strpos($constName, 'TYPE_') === 0) {
                 $this->constans[$constValue] = '$this->' . $constValue;
             }
         }
         $this->constans['smallint'] = '$this->smallInteger';
         $this->constans['bigint'] = '$this->bigInteger';
     }
     if ($column->type !== Schema::TYPE_BOOLEAN && $column->size !== null) {
         $size = [$column->size];
         if ($column->scale !== null) {
             $size[] = $column->scale;
         }
     } else {
         $size = [];
     }
     $result = '';
     if (isset($this->constans[$column->type])) {
         $result = $this->constans[$column->type] . '(' . implode(',', $size) . ')';
         if (!$column->allowNull) {
             $result .= '->notNull()';
         }
         if ($column->defaultValue !== null) {
             $default = is_string($column->defaultValue) ? "'" . addslashes($column->defaultValue) . "'" : $column->defaultValue;
             $result .= "->defaultValue({$default})";
         }
     } else {
         $result = $column->dbType;
         if (!empty($size)) {
             $result .= '(' . implode(',', $size) . ')';
         }
         if (!$column->allowNull) {
             $result .= ' NOT NULL';
         }
         if ($column->defaultValue !== null) {
             $default = is_string($column->defaultValue) ? "'" . addslashes($column->defaultValue) . "'" : $column->defaultValue;
             $result .= " DEFAULT {$default}";
         }
         $result = '"' . $result . '"';
     }
     return $result;
 }
Exemplo n.º 2
0
 /**
  *
  * @param \yii\db\ColumnSchema $column
  * @return string
  */
 public function getSchemaType($column)
 {
     if ($this->constans === null) {
         $this->constans = [];
         $ref = new \ReflectionClass(Schema::className());
         foreach ($ref->getConstants() as $constName => $constValue) {
             if (strpos($constName, 'TYPE_') === 0) {
                 $this->constans[$constValue] = 'Schema::' . $constName;
             }
         }
     }
     if ($column->type !== Schema::TYPE_BOOLEAN && $column->size !== null) {
         $size = [$column->size];
         if ($column->scale !== null) {
             $size[] = $column->scale;
         }
         $size = '(' . implode(',', $size) . ')';
     } else {
         $size = '';
     }
     if (isset($this->constans[$column->type])) {
         return [$this->constans[$column->type], $size];
     } else {
         return [$column->dbType, ''];
     }
 }