diff --git a/app/admin/model/MallCate.php b/app/admin/model/MallCate.php index 7c511c8..f8fc93a 100644 --- a/app/admin/model/MallCate.php +++ b/app/admin/model/MallCate.php @@ -5,7 +5,9 @@ namespace app\admin\model; use app\common\model\TimeModel; - +/** + * + */ class MallCate extends TimeModel { diff --git a/app/admin/service/curd/BuildCurdService.php b/app/admin/service/curd/BuildCurdService.php index 39edb30..657f66c 100644 --- a/app/admin/service/curd/BuildCurdService.php +++ b/app/admin/service/curd/BuildCurdService.php @@ -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'], + 'type' => $vo['Type'], + 'comment' => !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'], 'required' => $vo['Null'] == "NO" ? true : false, - 'default' => $vo['Default'], + '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(); // 构建数据 @@ -359,9 +365,10 @@ class BuildCurdService continue; } $colum = [ - 'type' => $vo['Type'], + 'type' => $vo['Type'], 'comment' => $vo['Comment'], 'default' => $vo['Default'], + 'field' => $vo['Field'] ]; $this->buildColum($colum); @@ -377,13 +384,13 @@ class BuildCurdService $modelName = array_pop($modelArray); $relation = [ - 'modelFilename' => $modelFilename, - 'modelName' => $modelName, - 'foreignKey' => $foreignKey, - 'primaryKey' => $primaryKey, + 'modelFilename' => $modelFilename, + 'modelName' => $modelName, + 'foreignKey' => $foreignKey, + 'primaryKey' => $primaryKey, 'bindSelectField' => $bindSelectField, - 'delete' => $delete, - 'tableColumns' => $formatColums, + 'delete' => $delete, + 'tableColumns' => $formatColums, ]; if (!empty($bindSelectField)) { $relationArray = explode('\\', $modelFilename); @@ -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; } @@ -734,7 +761,7 @@ class BuildCurdService $selectCode = $this->replaceTemplate( $this->getTemplate("model{$this->DS}select"), [ - 'name' => $name, + 'name' => $name, 'values' => $values, ] ); @@ -753,7 +780,7 @@ class BuildCurdService $optionCode = $this->replaceTemplate( $this->getTemplate("view{$this->DS}module{$this->DS}option"), [ - 'name' => $name, + 'name' => $name, 'select' => $select, ] ); @@ -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'], @@ -859,8 +888,8 @@ class BuildCurdService $optionCode = $this->replaceTemplate( $this->getTemplate("view{$this->DS}module{$this->DS}radioInput"), [ - 'field' => $field, - 'name' => $name, + 'field' => $field, + 'name' => $name, 'select' => $select, ] ); @@ -879,8 +908,8 @@ class BuildCurdService $optionCode = $this->replaceTemplate( $this->getTemplate("view{$this->DS}module{$this->DS}checkboxInput"), [ - 'field' => $field, - 'name' => $name, + 'field' => $field, + 'name' => $name, 'select' => $select, ] ); @@ -1103,13 +1132,13 @@ class BuildCurdService $controllerValue = $this->replaceTemplate( $this->getTemplate("controller{$this->DS}controller"), [ - 'controllerName' => $this->controllerName, - 'controllerNamespace' => $this->controllerNamespace, + 'controllerName' => $this->controllerName, + 'controllerNamespace' => $this->controllerNamespace, 'controllerAnnotation' => $this->tableComment, - 'modelFilename' => "\app\admin\model\\{$modelFilenameExtend}", - 'indexMethod' => $controllerIndexMethod, - 'exportMethod' => $controllerExportMethod, - 'selectList' => $selectList, + 'modelFilename' => "\app\admin\model\\{$modelFilenameExtend}", + 'indexMethod' => $controllerIndexMethod, + 'exportMethod' => $controllerExportMethod, + 'selectList' => $selectList, ] ); $this->fileList[$controllerFile] = $controllerValue; @@ -1134,9 +1163,9 @@ class BuildCurdService $this->getTemplate("model{$this->DS}relation"), [ 'relationMethod' => $relation, - 'relationModel' => "\app\admin\model\\{$val['modelFilename']}", - 'foreignKey' => $val['foreignKey'], - 'primaryKey' => $val['primaryKey'], + 'relationModel' => "\app\admin\model\\{$val['modelFilename']}", + 'foreignKey' => $val['foreignKey'], + 'primaryKey' => $val['primaryKey'], ] ); $relationList .= $relationCode; @@ -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) { @@ -1161,12 +1197,13 @@ class BuildCurdService $modelValue = $this->replaceTemplate( $this->getTemplate("model{$this->DS}model"), [ - 'modelName' => $this->modelName, + 'modelName' => $this->modelName, 'modelNamespace' => "app\admin\model{$extendNamespace}", - 'table' => $this->table, - 'deleteTime' => $this->delete ? '"delete_time"' : 'false', - 'relationList' => $relationList, - 'selectList' => $selectList, + 'table' => $this->table, + 'deleteTime' => $this->delete ? '"delete_time"' : 'false', + 'relationList' => $relationList, + 'selectList' => $selectList, + 'doc_content' => $doc_content, ] ); $this->fileList[$modelFile] = $modelValue; @@ -1194,12 +1231,12 @@ class BuildCurdService $relationModelValue = $this->replaceTemplate( $this->getTemplate("model{$this->DS}model"), [ - 'modelName' => $val['modelName'], + 'modelName' => $val['modelName'], 'modelNamespace' => "app\admin\model{$extendNamespace}", - 'table' => $key, - 'deleteTime' => $val['delete'] ? '"delete_time"' : 'false', - 'relationList' => '', - 'selectList' => '', + 'table' => $key, + 'deleteTime' => $val['delete'] ? '"delete_time"' : 'false', + 'relationList' => '', + 'selectList' => '', ] ); $this->fileList[$relationModelFile] = $relationModelValue; @@ -1302,12 +1339,12 @@ class BuildCurdService $addFormList .= $this->replaceTemplate( $this->getTemplate($templateFile), [ - 'comment' => $val['comment'], - 'field' => $field, + 'comment' => $val['comment'], + 'field' => $field, 'required' => $this->buildRequiredHtml($val['required']), - 'required_text' => $val['required'] ? '1' : '', - 'value' => $val['default'], - 'define' => $define, + 'required_text' => $val['required'] ? '1' : '', + 'value' => $val['default'], + 'define' => $define, ] ); } @@ -1400,12 +1437,12 @@ class BuildCurdService $editFormList .= $this->replaceTemplate( $this->getTemplate($templateFile), [ - 'comment' => $val['comment'], - 'field' => $field, + 'comment' => $val['comment'], + 'field' => $field, 'required' => $this->buildRequiredHtml($val['required']), - 'required_text' => $val['required'] ? '1' : '', - 'value' => $value, - 'define' => $define, + 'required_text' => $val['required'] ? '1' : '', + 'value' => $value, + 'define' => $define, ] ); } @@ -1508,7 +1545,7 @@ class BuildCurdService $this->getTemplate("static{$this->DS}js"), [ 'controllerUrl' => $this->controllerUrl, - 'indexCols' => $indexCols, + 'indexCols' => $indexCols, ] ); $this->fileList[$jsFile] = $jsValue; @@ -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'; + } +} \ No newline at end of file diff --git a/app/admin/service/curd/templates/model/model.code b/app/admin/service/curd/templates/model/model.code index 5e5232c..31dd34c 100644 --- a/app/admin/service/curd/templates/model/model.code +++ b/app/admin/service/curd/templates/model/model.code @@ -4,6 +4,9 @@ namespace {{modelNamespace}}; use app\common\model\TimeModel; +/** +{{doc_content}} + */ class {{modelName}} extends TimeModel {