作者 chenwei6

一键生成CRUD 多数据库支持 --db=xxx xxx为tp5中配置的数据库key

@@ -173,12 +173,15 @@ class Crud extends Command @@ -173,12 +173,15 @@ class Crud extends Command
173 ->addOption('sortfield', null, Option::VALUE_OPTIONAL, 'sort field', null) 173 ->addOption('sortfield', null, Option::VALUE_OPTIONAL, 'sort field', null)
174 ->addOption('headingfilterfield', null, Option::VALUE_OPTIONAL, 'heading filter field', null) 174 ->addOption('headingfilterfield', null, Option::VALUE_OPTIONAL, 'heading filter field', null)
175 ->addOption('editorclass', null, Option::VALUE_OPTIONAL, 'automatically generate editor class', null) 175 ->addOption('editorclass', null, Option::VALUE_OPTIONAL, 'automatically generate editor class', null)
  176 + ->addOption('db', null, Option::VALUE_OPTIONAL, 'database config name', 'database')
176 ->setDescription('Build CRUD controller and model from table'); 177 ->setDescription('Build CRUD controller and model from table');
177 } 178 }
178 179
179 protected function execute(Input $input, Output $output) 180 protected function execute(Input $input, Output $output)
180 { 181 {
181 $adminPath = dirname(__DIR__) . DS; 182 $adminPath = dirname(__DIR__) . DS;
  183 + //数据库
  184 + $db = $input->getOption('db');
182 //表名 185 //表名
183 $table = $input->getOption('table') ?: ''; 186 $table = $input->getOption('table') ?: '';
184 //自定义控制器 187 //自定义控制器
@@ -279,8 +282,9 @@ class Crud extends Command @@ -279,8 +282,9 @@ class Crud extends Command
279 282
280 $this->reservedField = array_merge($this->reservedField, [$this->createTimeField, $this->updateTimeField, $this->deleteTimeField]); 283 $this->reservedField = array_merge($this->reservedField, [$this->createTimeField, $this->updateTimeField, $this->deleteTimeField]);
281 284
282 - $dbname = Config::get('database.database');  
283 - $prefix = Config::get('database.prefix'); 285 + $dbconnect = Db::connect($db);
  286 + $dbname = Config::get($db . '.database');
  287 + $prefix = Config::get($db . '.prefix');
284 288
285 //模块 289 //模块
286 $moduleName = 'admin'; 290 $moduleName = 'admin';
@@ -291,11 +295,11 @@ class Crud extends Command @@ -291,11 +295,11 @@ class Crud extends Command
291 $modelName = $table = stripos($table, $prefix) === 0 ? substr($table, strlen($prefix)) : $table; 295 $modelName = $table = stripos($table, $prefix) === 0 ? substr($table, strlen($prefix)) : $table;
292 $modelTableType = 'table'; 296 $modelTableType = 'table';
293 $modelTableTypeName = $modelTableName = $modelName; 297 $modelTableTypeName = $modelTableName = $modelName;
294 - $modelTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$modelTableName}'", [], true); 298 + $modelTableInfo = $dbconnect->query("SHOW TABLE STATUS LIKE '{$modelTableName}'", [], true);
295 if (!$modelTableInfo) { 299 if (!$modelTableInfo) {
296 $modelTableType = 'name'; 300 $modelTableType = 'name';
297 $modelTableName = $prefix . $modelName; 301 $modelTableName = $prefix . $modelName;
298 - $modelTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$modelTableName}'", [], true); 302 + $modelTableInfo = $dbconnect->query("SHOW TABLE STATUS LIKE '{$modelTableName}'", [], true);
299 if (!$modelTableInfo) { 303 if (!$modelTableInfo) {
300 throw new Exception("table not found"); 304 throw new Exception("table not found");
301 } 305 }
@@ -312,11 +316,11 @@ class Crud extends Command @@ -312,11 +316,11 @@ class Crud extends Command
312 $relationName = stripos($relationTable, $prefix) === 0 ? substr($relationTable, strlen($prefix)) : $relationTable; 316 $relationName = stripos($relationTable, $prefix) === 0 ? substr($relationTable, strlen($prefix)) : $relationTable;
313 $relationTableType = 'table'; 317 $relationTableType = 'table';
314 $relationTableTypeName = $relationTableName = $relationName; 318 $relationTableTypeName = $relationTableName = $relationName;
315 - $relationTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], true); 319 + $relationTableInfo = $dbconnect->query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], true);
316 if (!$relationTableInfo) { 320 if (!$relationTableInfo) {
317 $relationTableType = 'name'; 321 $relationTableType = 'name';
318 $relationTableName = $prefix . $relationName; 322 $relationTableName = $prefix . $relationName;
319 - $relationTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], true); 323 + $relationTableInfo = $dbconnect->query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], true);
320 if (!$relationTableInfo) { 324 if (!$relationTableInfo) {
321 throw new Exception("relation table not found"); 325 throw new Exception("relation table not found");
322 } 326 }
@@ -451,7 +455,7 @@ class Crud extends Command @@ -451,7 +455,7 @@ class Crud extends Command
451 . "WHERE TABLE_SCHEMA = ? AND table_name = ? " 455 . "WHERE TABLE_SCHEMA = ? AND table_name = ? "
452 . "ORDER BY ORDINAL_POSITION"; 456 . "ORDER BY ORDINAL_POSITION";
453 //加载主表的列 457 //加载主表的列
454 - $columnList = Db::query($sql, [$dbname, $modelTableName]); 458 + $columnList = $dbconnect->query($sql, [$dbname, $modelTableName]);
455 $fieldArr = []; 459 $fieldArr = [];
456 foreach ($columnList as $k => $v) { 460 foreach ($columnList as $k => $v) {
457 $fieldArr[] = $v['COLUMN_NAME']; 461 $fieldArr[] = $v['COLUMN_NAME'];
@@ -459,7 +463,7 @@ class Crud extends Command @@ -459,7 +463,7 @@ class Crud extends Command
459 463
460 // 加载关联表的列 464 // 加载关联表的列
461 foreach ($relations as $index => &$relation) { 465 foreach ($relations as $index => &$relation) {
462 - $relationColumnList = Db::query($sql, [$dbname, $relation['relationTableName']]); 466 + $relationColumnList = $dbconnect->query($sql, [$dbname, $relation['relationTableName']]);
463 467
464 $relationFieldList = []; 468 $relationFieldList = [];
465 foreach ($relationColumnList as $k => $v) { 469 foreach ($relationColumnList as $k => $v) {
@@ -819,6 +823,7 @@ class Crud extends Command @@ -819,6 +823,7 @@ class Crud extends Command
819 } 823 }
820 824
821 $data = [ 825 $data = [
  826 + 'databaseConfigName' => $db,
822 'controllerNamespace' => $controllerNamespace, 827 'controllerNamespace' => $controllerNamespace,
823 'modelNamespace' => $modelNamespace, 828 'modelNamespace' => $modelNamespace,
824 'validateNamespace' => $validateNamespace, 829 'validateNamespace' => $validateNamespace,
@@ -10,6 +10,8 @@ class {%modelName%} extends Model @@ -10,6 +10,8 @@ class {%modelName%} extends Model
10 10
11 {%softDelete%} 11 {%softDelete%}
12 12
  13 + //数据库
  14 + protected $connection = '{%databaseConfigName%}';
13 // 表名 15 // 表名
14 protected ${%modelTableType%} = '{%modelTableTypeName%}'; 16 protected ${%modelTableType%} = '{%modelTableTypeName%}';
15 17