diff --git a/extend/base/common/service/scheme/DbToSchemeService.php b/extend/base/common/service/scheme/DbToSchemeService.php index 2ebfac2..5b45be9 100644 --- a/extend/base/common/service/scheme/DbToSchemeService.php +++ b/extend/base/common/service/scheme/DbToSchemeService.php @@ -150,22 +150,69 @@ class DbToSchemeService $componentAttr = "\n #[Component(type: '$compType', options: $optionsCode)]"; } - // 构建 Field 注解 - $defaultStr = is_null($default) ? 'null' : (is_string($default) ? "'$default'" : $default); - $nullable = $notNull ? 'false' : 'true'; - $unsigned = str_contains($type, 'unsigned') ? 'true' : 'false'; - $primaryStr = $primary ? 'true' : 'false'; - $autoincStr = $autoinc ? 'true' : 'false'; - // 如果是整型且未指定长度,默认11 (兼容旧习俗) if (str_contains($dbType, 'int') && !$length) { $length = 11; } - $lengthStr = is_null($length) ? 'null' : $length; + // 构建 Field 注解参数 + $fieldParams = []; + + // type (default: varchar) + if ($dbType !== 'varchar') { + $fieldParams[] = "type: '$dbType'"; + } + + // length (default: null) + if ($length !== null) { + $fieldParams[] = "length: $length"; + } + + // precision (default: 0) + if ($precision !== 0) { + $fieldParams[] = "precision: $precision"; + } + + // scale (default: 0) + if ($scale !== 0) { + $fieldParams[] = "scale: $scale"; + } + + // nullable (default: true) + if ($notNull) { + $fieldParams[] = "nullable: false"; + } + + // default (default: null) + if (!is_null($default)) { + $defaultVal = is_string($default) ? "'$default'" : $default; + $fieldParams[] = "default: $defaultVal"; + } + + // comment (default: '') + if ($cleanComment !== '') { + $fieldParams[] = "comment: '$cleanComment'"; + } + + // unsigned (default: false) + if (str_contains($type, 'unsigned')) { + $fieldParams[] = "unsigned: true"; + } + + // autoIncrement (default: false) + if ($autoinc) { + $fieldParams[] = "autoIncrement: true"; + } + + // primary (default: false) + if ($primary) { + $fieldParams[] = "primary: true"; + } + + $fieldAttrStr = implode(', ', $fieldParams); $fieldsCode[] = <<