增加生成模型字段注解

This commit is contained in:
2023-03-29 15:30:37 +08:00
parent 877867df80
commit 45509df381
3 changed files with 145 additions and 54 deletions

View File

@@ -5,7 +5,9 @@ namespace app\admin\model;
use app\common\model\TimeModel;
/**
*
*/
class MallCate extends TimeModel
{

View File

@@ -265,13 +265,14 @@ class BuildCurdService
// 获取表列注释
$colums = Db::query("SHOW FULL COLUMNS FROM {$this->tablePrefix}{$this->table}");
foreach ($colums as $vo) {
foreach ($colums as $vo) {
$colum = [
'type' => $vo['Type'],
'comment' => !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'],
'required' => $vo['Null'] == "NO" ? true : false,
'default' => $vo['Default'],
'field' => $vo['Field']
];
// 格式化列数据
@@ -297,6 +298,11 @@ class BuildCurdService
// 初始化默认模型名
$this->modelFilename = Str::studly($this->table);
// 主表模型命名
$modelArray = explode($this->DS, $this->modelFilename);
$this->modelName = array_pop($modelArray);
$this->buildViewJsUrl();
// 构建数据
@@ -362,6 +368,7 @@ class BuildCurdService
'type' => $vo['Type'],
'comment' => $vo['Comment'],
'default' => $vo['Default'],
'field' => $vo['Field']
];
$this->buildColum($colum);
@@ -608,10 +615,7 @@ class BuildCurdService
$namespaceSuffix = implode('\\', $namespaceArray);
$this->controllerNamespace = empty($namespaceSuffix) ? "app\admin\controller" : "app\admin\controller\\{$namespaceSuffix}";
// 主表模型命名
$modelArray = explode($this->DS, $this->modelFilename);
$this->modelName = array_pop($modelArray);
return $this;
}
@@ -691,6 +695,29 @@ class BuildCurdService
$colum['comment'] = trim($colum['comment']);
$colum['property_type'] = $this->fieldTypeToVarType($colum['type']);
$colum['property_name'] = $colum['field'];
$colum['data_list'] = '';
if (isset($colum['formType'])) {
if ($colum['formType'] == 'relation') {
$relation_model_name = '\\app\\admin\\model\\' . Str::studly($colum['define']['table']);
$colum['property_type'] = $relation_model_name;
$colum['property_name'] = Str::camel($colum['define']['table']);
} else if (in_array($colum['formType'], ['select', 'switch', 'radio', 'checkbox',])) {
$data_list = '';
foreach ($colum['define'] as $define_key => $define_value) {
$data_list .= $define_key . ':' . $define_value . ',';
}
$data_list = substr($data_list, 0, -1);
$colum['data_list'] = $data_list;
}
}
return $colum;
}
@@ -816,10 +843,12 @@ class BuildCurdService
protected function buildTableView($field, $options, $value)
{
$default_define = [
'table' => '', // 必填
'table' => '',
// 必填
'type' => 'checkbox',
'valueField' => 'id',
'fieldName' => 'title', // 必填
'fieldName' => 'title',
// 必填
'comment' => $options['comment'],
'field' => $field,
'required' => $options['required'],
@@ -1145,12 +1174,19 @@ class BuildCurdService
$selectList = '';
$doc_content = '';
foreach ($this->tableColumns as $field => $val) {
if (isset($val['formType']) && in_array($val['formType'], ['select', 'switch', 'radio', 'checkbox']) && isset($val['define'])) {
$selectList .= $this->buildSelectModel($field, $val['define']);
}
$doc_content .= " * @property {$val['property_type']} \${$val['property_name']} {$val['comment']} {$val['data_list']}\n";
}
$doc_content = substr($doc_content, 0, -1);
$extendNamespaceArray = explode($this->DS, $this->modelFilename);
$extendNamespace = null;
if (count($extendNamespaceArray) > 1) {
@@ -1167,6 +1203,7 @@ class BuildCurdService
'deleteTime' => $this->delete ? '"delete_time"' : 'false',
'relationList' => $relationList,
'selectList' => $selectList,
'doc_content' => $doc_content,
]
);
$this->fileList[$modelFile] = $modelValue;
@@ -1648,4 +1685,53 @@ class BuildCurdService
}
return $string;
}
public function fieldTypeToVarType($type)
{
$type_prefix_map = [
'BIT' => 'bool',
'TINYINT' => 'int',
'BOOL' => 'bool',
'BOOLEAN' => 'bool',
'SMALLINT' => 'int',
'MEDIUMINT' => 'int',
'INT' => 'int',
'INTEGER' => 'int',
'BIGINT' => 'int',
'FLOAT' => 'float|double',
'DOUBLE' => 'float|double',
'DECIMAL' => 'float|double',
'DATE' => 'string',
'DATETIME' => 'string',
'TIMESTAMP' => 'int',
'TIME' => 'string',
'YEAR' => 'int',
'CHAR' => 'string',
'VARCHAR' => 'string',
'BINARY' => 'string',
'VARBINARY' => 'string',
'TINYBLOB' => 'string',
'BLOB' => 'string',
'MEDIUMBLOB' => 'string',
'LONGBLOB' => 'string',
'TINYTEXT' => 'string',
'TEXT' => 'string',
'MEDIUMTEXT' => 'string',
'LONGTEXT' => 'string',
'ENUM' => 'string',
'SET' => 'string',
'JSON' => 'string',
];
foreach ($type_prefix_map as $sql_type => $var_type) {
if (Str::startsWith(strtolower($type), strtolower($sql_type))) {
return $var_type;
}
}
return 'mixed';
}
}

View File

@@ -4,6 +4,9 @@ namespace {{modelNamespace}};
use app\common\model\TimeModel;
/**
{{doc_content}}
*/
class {{modelName}} extends TimeModel
{