跨数据库生成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,6 +293,7 @@ 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 = [
@@ -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'],