From d1b386dd084c41c4d6d80bab755d0d208fd4eb81 Mon Sep 17 00:00:00 2001
From: chenwei6 <chenwei6@le.com>
Date: Wed, 27 Mar 2019 11:49:35 +0800
Subject: [PATCH] 一键生成CRUD 多数据库支持 --db=xxx xxx为tp5中配置的数据库key

---
 application/admin/command/Crud.php              | 21 +++++++++++++--------
 application/admin/command/Crud/stubs/model.stub |  2 ++
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/application/admin/command/Crud.php b/application/admin/command/Crud.php
index 8eed578..b960485 100755
--- a/application/admin/command/Crud.php
+++ b/application/admin/command/Crud.php
@@ -173,12 +173,15 @@ class Crud extends Command
             ->addOption('sortfield', null, Option::VALUE_OPTIONAL, 'sort field', null)
             ->addOption('headingfilterfield', null, Option::VALUE_OPTIONAL, 'heading filter field', null)
             ->addOption('editorclass', null, Option::VALUE_OPTIONAL, 'automatically generate editor class', null)
+            ->addOption('db', null, Option::VALUE_OPTIONAL, 'database config name', 'database')
             ->setDescription('Build CRUD controller and model from table');
     }
 
     protected function execute(Input $input, Output $output)
     {
         $adminPath = dirname(__DIR__) . DS;
+        //数据库
+        $db = $input->getOption('db');
         //表名
         $table = $input->getOption('table') ?: '';
         //自定义控制器
@@ -279,8 +282,9 @@ class Crud extends Command
 
         $this->reservedField = array_merge($this->reservedField, [$this->createTimeField, $this->updateTimeField, $this->deleteTimeField]);
 
-        $dbname = Config::get('database.database');
-        $prefix = Config::get('database.prefix');
+        $dbconnect = Db::connect($db);
+        $dbname = Config::get($db . '.database');
+        $prefix = Config::get($db . '.prefix');
 
         //模块
         $moduleName = 'admin';
@@ -291,11 +295,11 @@ class Crud extends Command
         $modelName = $table = stripos($table, $prefix) === 0 ? substr($table, strlen($prefix)) : $table;
         $modelTableType = 'table';
         $modelTableTypeName = $modelTableName = $modelName;
-        $modelTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$modelTableName}'", [], true);
+        $modelTableInfo = $dbconnect->query("SHOW TABLE STATUS LIKE '{$modelTableName}'", [], true);
         if (!$modelTableInfo) {
             $modelTableType = 'name';
             $modelTableName = $prefix . $modelName;
-            $modelTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$modelTableName}'", [], true);
+            $modelTableInfo = $dbconnect->query("SHOW TABLE STATUS LIKE '{$modelTableName}'", [], true);
             if (!$modelTableInfo) {
                 throw new Exception("table not found");
             }
@@ -312,11 +316,11 @@ class Crud extends Command
                 $relationName = stripos($relationTable, $prefix) === 0 ? substr($relationTable, strlen($prefix)) : $relationTable;
                 $relationTableType = 'table';
                 $relationTableTypeName = $relationTableName = $relationName;
-                $relationTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], true);
+                $relationTableInfo = $dbconnect->query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], true);
                 if (!$relationTableInfo) {
                     $relationTableType = 'name';
                     $relationTableName = $prefix . $relationName;
-                    $relationTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], true);
+                    $relationTableInfo = $dbconnect->query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], true);
                     if (!$relationTableInfo) {
                         throw new Exception("relation table not found");
                     }
@@ -451,7 +455,7 @@ class Crud extends Command
             . "WHERE TABLE_SCHEMA = ? AND table_name = ? "
             . "ORDER BY ORDINAL_POSITION";
         //加载主表的列
-        $columnList = Db::query($sql, [$dbname, $modelTableName]);
+        $columnList = $dbconnect->query($sql, [$dbname, $modelTableName]);
         $fieldArr = [];
         foreach ($columnList as $k => $v) {
             $fieldArr[] = $v['COLUMN_NAME'];
@@ -459,7 +463,7 @@ class Crud extends Command
 
         // 加载关联表的列
         foreach ($relations as $index => &$relation) {
-            $relationColumnList = Db::query($sql, [$dbname, $relation['relationTableName']]);
+            $relationColumnList = $dbconnect->query($sql, [$dbname, $relation['relationTableName']]);
 
             $relationFieldList = [];
             foreach ($relationColumnList as $k => $v) {
@@ -819,6 +823,7 @@ class Crud extends Command
             }
 
             $data = [
+                'databaseConfigName'      => $db,
                 'controllerNamespace'     => $controllerNamespace,
                 'modelNamespace'          => $modelNamespace,
                 'validateNamespace'       => $validateNamespace,
diff --git a/application/admin/command/Crud/stubs/model.stub b/application/admin/command/Crud/stubs/model.stub
index ff0372c..8b8182f 100755
--- a/application/admin/command/Crud/stubs/model.stub
+++ b/application/admin/command/Crud/stubs/model.stub
@@ -10,6 +10,8 @@ class {%modelName%} extends Model
 
     {%softDelete%}
 
+    //数据库
+    protected $connection = '{%databaseConfigName%}';
     // 表名
     protected ${%modelTableType%} = '{%modelTableTypeName%}';
     
--
libgit2 0.24.0