跨数据库生成curd

This commit is contained in:
2023-01-12 11:05:38 +08:00
parent 5f1124426c
commit ccde27366a

View File

@@ -4,6 +4,7 @@
namespace app\admin\service\curd; namespace app\admin\service\curd;
use app\admin\service\curd\exceptions\TableException; use app\admin\service\curd\exceptions\TableException;
use Phinx\Db\Adapter\AdapterFactory;
use think\exception\FileException; use think\exception\FileException;
use think\facade\Db; use think\facade\Db;
use think\helper\Str; use think\helper\Str;
@@ -226,17 +227,44 @@ class BuildCurdService
*/ */
protected $formTypeArray = ['text', 'image', 'images', 'file', 'files', 'select', 'switch', 'date', 'editor', 'textarea', 'checkbox', 'radio', 'relation', 'table', 'city', 'tag']; protected $formTypeArray = ['text', 'image', 'images', 'file', 'files', 'select', 'switch', 'date', 'editor', 'textarea', 'checkbox', 'radio', 'relation', 'table', 'city', 'tag'];
protected $dbConfig;
protected $dbMigrate;
/** /**
* 初始化 * 初始化
* BuildCurd constructor. * BuildCurd constructor.
*/ */
public function __construct() public function __construct()
{ {
$this->tablePrefix = config('database.connections.mysql.prefix'); $default_conn = config('database.default');
$this->dbName = config('database.connections.mysql.database'); $this->tablePrefix = config("database.connections.{$default_conn}.prefix");
$this->dbName = config("database.connections.{$default_conn}.database");
$this->dir = __DIR__; $this->dir = __DIR__;
$this->rootDir = root_path(); $this->rootDir = root_path();
return $this;
$this->dbConfig = [
'adapter' => config("database.connections.{$default_conn}.type"),
'host' => config("database.connections.{$default_conn}.hostname"),
'name' => config("database.connections.{$default_conn}.database"),
'user' => config("database.connections.{$default_conn}.username"),
'pass' => config("database.connections.{$default_conn}.password"),
'port' => config("database.connections.{$default_conn}.hostport"),
'charset' => config("database.connections.{$default_conn}.charset"),
'table_prefix' => config("database.connections.{$default_conn}.prefix"),
];
$this->dbConfig['default_migration_table'] = $this->dbConfig['table_prefix'] . config('database.migration_table', 'migrations');
$adapter = AdapterFactory::instance()->getAdapter($this->dbConfig['adapter'], $this->dbConfig);
if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
$adapter = AdapterFactory::instance()->getWrapper('prefix', $adapter);
}
$this->dbMigrate = $adapter;
} }
public function getTableColumns() public function getTableColumns()
@@ -265,13 +293,14 @@ class BuildCurdService
// 获取表列注释 // 获取表列注释
$colums = Db::query("SHOW FULL COLUMNS FROM {$this->tablePrefix}{$this->table}"); $colums = Db::query("SHOW FULL COLUMNS FROM {$this->tablePrefix}{$this->table}");
foreach ($colums as $vo) { foreach ($colums as $vo) {
$colum = [ $colum = [
'type' => $vo['Type'], 'type' => $vo['Type'],
'comment' => !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'], 'comment' => !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'],
'required' => $vo['Null'] == "NO" ? true : false, 'required' => $vo['Null'] == "NO" ? true : false,
'default' => $vo['Default'], 'default' => $vo['Default'],
]; ];
// 格式化列数据 // 格式化列数据
@@ -359,7 +388,7 @@ class BuildCurdService
continue; continue;
} }
$colum = [ $colum = [
'type' => $vo['Type'], 'type' => $vo['Type'],
'comment' => $vo['Comment'], 'comment' => $vo['Comment'],
'default' => $vo['Default'], 'default' => $vo['Default'],
]; ];
@@ -377,13 +406,13 @@ class BuildCurdService
$modelName = array_pop($modelArray); $modelName = array_pop($modelArray);
$relation = [ $relation = [
'modelFilename' => $modelFilename, 'modelFilename' => $modelFilename,
'modelName' => $modelName, 'modelName' => $modelName,
'foreignKey' => $foreignKey, 'foreignKey' => $foreignKey,
'primaryKey' => $primaryKey, 'primaryKey' => $primaryKey,
'bindSelectField' => $bindSelectField, 'bindSelectField' => $bindSelectField,
'delete' => $delete, 'delete' => $delete,
'tableColumns' => $formatColums, 'tableColumns' => $formatColums,
]; ];
if (!empty($bindSelectField)) { if (!empty($bindSelectField)) {
$relationArray = explode('\\', $modelFilename); $relationArray = explode('\\', $modelFilename);
@@ -734,7 +763,7 @@ class BuildCurdService
$selectCode = $this->replaceTemplate( $selectCode = $this->replaceTemplate(
$this->getTemplate("model{$this->DS}select"), $this->getTemplate("model{$this->DS}select"),
[ [
'name' => $name, 'name' => $name,
'values' => $values, 'values' => $values,
] ]
); );
@@ -753,7 +782,7 @@ class BuildCurdService
$optionCode = $this->replaceTemplate( $optionCode = $this->replaceTemplate(
$this->getTemplate("view{$this->DS}module{$this->DS}option"), $this->getTemplate("view{$this->DS}module{$this->DS}option"),
[ [
'name' => $name, 'name' => $name,
'select' => $select, 'select' => $select,
] ]
); );
@@ -816,10 +845,12 @@ class BuildCurdService
protected function buildTableView($field, $options, $value) protected function buildTableView($field, $options, $value)
{ {
$default_define = [ $default_define = [
'table' => '', // 必填 'table' => '',
// 必填
'type' => 'checkbox', 'type' => 'checkbox',
'valueField' => 'id', 'valueField' => 'id',
'fieldName' => 'title', // 必填 'fieldName' => 'title',
// 必填
'comment' => $options['comment'], 'comment' => $options['comment'],
'field' => $field, 'field' => $field,
'required' => $options['required'], 'required' => $options['required'],
@@ -859,8 +890,8 @@ class BuildCurdService
$optionCode = $this->replaceTemplate( $optionCode = $this->replaceTemplate(
$this->getTemplate("view{$this->DS}module{$this->DS}radioInput"), $this->getTemplate("view{$this->DS}module{$this->DS}radioInput"),
[ [
'field' => $field, 'field' => $field,
'name' => $name, 'name' => $name,
'select' => $select, 'select' => $select,
] ]
); );
@@ -879,8 +910,8 @@ class BuildCurdService
$optionCode = $this->replaceTemplate( $optionCode = $this->replaceTemplate(
$this->getTemplate("view{$this->DS}module{$this->DS}checkboxInput"), $this->getTemplate("view{$this->DS}module{$this->DS}checkboxInput"),
[ [
'field' => $field, 'field' => $field,
'name' => $name, 'name' => $name,
'select' => $select, 'select' => $select,
] ]
); );
@@ -1103,13 +1134,13 @@ class BuildCurdService
$controllerValue = $this->replaceTemplate( $controllerValue = $this->replaceTemplate(
$this->getTemplate("controller{$this->DS}controller"), $this->getTemplate("controller{$this->DS}controller"),
[ [
'controllerName' => $this->controllerName, 'controllerName' => $this->controllerName,
'controllerNamespace' => $this->controllerNamespace, 'controllerNamespace' => $this->controllerNamespace,
'controllerAnnotation' => $this->tableComment, 'controllerAnnotation' => $this->tableComment,
'modelFilename' => "\app\admin\model\\{$modelFilenameExtend}", 'modelFilename' => "\app\admin\model\\{$modelFilenameExtend}",
'indexMethod' => $controllerIndexMethod, 'indexMethod' => $controllerIndexMethod,
'exportMethod' => $controllerExportMethod, 'exportMethod' => $controllerExportMethod,
'selectList' => $selectList, 'selectList' => $selectList,
] ]
); );
$this->fileList[$controllerFile] = $controllerValue; $this->fileList[$controllerFile] = $controllerValue;
@@ -1134,9 +1165,9 @@ class BuildCurdService
$this->getTemplate("model{$this->DS}relation"), $this->getTemplate("model{$this->DS}relation"),
[ [
'relationMethod' => $relation, 'relationMethod' => $relation,
'relationModel' => "\app\admin\model\\{$val['modelFilename']}", 'relationModel' => "\app\admin\model\\{$val['modelFilename']}",
'foreignKey' => $val['foreignKey'], 'foreignKey' => $val['foreignKey'],
'primaryKey' => $val['primaryKey'], 'primaryKey' => $val['primaryKey'],
] ]
); );
$relationList .= $relationCode; $relationList .= $relationCode;
@@ -1161,12 +1192,12 @@ class BuildCurdService
$modelValue = $this->replaceTemplate( $modelValue = $this->replaceTemplate(
$this->getTemplate("model{$this->DS}model"), $this->getTemplate("model{$this->DS}model"),
[ [
'modelName' => $this->modelName, 'modelName' => $this->modelName,
'modelNamespace' => "app\admin\model{$extendNamespace}", 'modelNamespace' => "app\admin\model{$extendNamespace}",
'table' => $this->table, 'table' => $this->table,
'deleteTime' => $this->delete ? '"delete_time"' : 'false', 'deleteTime' => $this->delete ? '"delete_time"' : 'false',
'relationList' => $relationList, 'relationList' => $relationList,
'selectList' => $selectList, 'selectList' => $selectList,
] ]
); );
$this->fileList[$modelFile] = $modelValue; $this->fileList[$modelFile] = $modelValue;
@@ -1194,12 +1225,12 @@ class BuildCurdService
$relationModelValue = $this->replaceTemplate( $relationModelValue = $this->replaceTemplate(
$this->getTemplate("model{$this->DS}model"), $this->getTemplate("model{$this->DS}model"),
[ [
'modelName' => $val['modelName'], 'modelName' => $val['modelName'],
'modelNamespace' => "app\admin\model{$extendNamespace}", 'modelNamespace' => "app\admin\model{$extendNamespace}",
'table' => $key, 'table' => $key,
'deleteTime' => $val['delete'] ? '"delete_time"' : 'false', 'deleteTime' => $val['delete'] ? '"delete_time"' : 'false',
'relationList' => '', 'relationList' => '',
'selectList' => '', 'selectList' => '',
] ]
); );
$this->fileList[$relationModelFile] = $relationModelValue; $this->fileList[$relationModelFile] = $relationModelValue;
@@ -1302,12 +1333,12 @@ class BuildCurdService
$addFormList .= $this->replaceTemplate( $addFormList .= $this->replaceTemplate(
$this->getTemplate($templateFile), $this->getTemplate($templateFile),
[ [
'comment' => $val['comment'], 'comment' => $val['comment'],
'field' => $field, 'field' => $field,
'required' => $this->buildRequiredHtml($val['required']), 'required' => $this->buildRequiredHtml($val['required']),
'required_text' => $val['required'] ? '1' : '', 'required_text' => $val['required'] ? '1' : '',
'value' => $val['default'], 'value' => $val['default'],
'define' => $define, 'define' => $define,
] ]
); );
} }
@@ -1400,12 +1431,12 @@ class BuildCurdService
$editFormList .= $this->replaceTemplate( $editFormList .= $this->replaceTemplate(
$this->getTemplate($templateFile), $this->getTemplate($templateFile),
[ [
'comment' => $val['comment'], 'comment' => $val['comment'],
'field' => $field, 'field' => $field,
'required' => $this->buildRequiredHtml($val['required']), 'required' => $this->buildRequiredHtml($val['required']),
'required_text' => $val['required'] ? '1' : '', 'required_text' => $val['required'] ? '1' : '',
'value' => $value, 'value' => $value,
'define' => $define, 'define' => $define,
] ]
); );
} }
@@ -1508,7 +1539,7 @@ class BuildCurdService
$this->getTemplate("static{$this->DS}js"), $this->getTemplate("static{$this->DS}js"),
[ [
'controllerUrl' => $this->controllerUrl, 'controllerUrl' => $this->controllerUrl,
'indexCols' => $indexCols, 'indexCols' => $indexCols,
] ]
); );
$this->fileList[$jsFile] = $jsValue; $this->fileList[$jsFile] = $jsValue;
@@ -1648,4 +1679,4 @@ class BuildCurdService
} }
return $string; return $string;
} }
} }