正在显示
1 个修改的文件
包含
12 行增加
和
8 行删除
| @@ -104,30 +104,34 @@ class Demo extends Command | @@ -104,30 +104,34 @@ class Demo extends Command | ||
| 104 | public function handle(){ | 104 | public function handle(){ |
| 105 | return $this->initTable(1380,1862); | 105 | return $this->initTable(1380,1862); |
| 106 | } | 106 | } |
| 107 | - | ||
| 108 | - public function initTable($project_id,$news_project_id) | 107 | + |
| 108 | + public function initTable($project_id, $news_project_id) | ||
| 109 | { | 109 | { |
| 110 | - config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_'.$project_id]); | 110 | + config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_' . $project_id]); |
| 111 | $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName(); | 111 | $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName(); |
| 112 | $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables(); | 112 | $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables(); |
| 113 | $tables = array_column($tables, 'Tables_in_' . $database_name); | 113 | $tables = array_column($tables, 'Tables_in_' . $database_name); |
| 114 | + | ||
| 114 | foreach ($tables as $table) { | 115 | foreach ($tables as $table) { |
| 115 | $has_table = Schema::connection('custom_mysql')->hasTable($table); | 116 | $has_table = Schema::connection('custom_mysql')->hasTable($table); |
| 117 | + | ||
| 116 | if (!$has_table) { | 118 | if (!$has_table) { |
| 117 | $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}"); | 119 | $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}"); |
| 118 | - DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']); | 120 | + DB::connection('custom_mysql')->statement(array_values((array)$sql[0])[0]); // 修正此处的sql语句获取方式 |
| 119 | } | 121 | } |
| 120 | - if($table == 'gl_customer_visit' || $table == 'gl_customer_visit_item' || $table == 'gl_inquiry_other' || $table == 'gl_inquiry_form_data' || $table == 'gl_inquiry_form'){ | 122 | + |
| 123 | + if ($table == 'gl_customer_visit' || $table == 'gl_customer_visit_item' || $table == 'gl_inquiry_other' || $table == 'gl_inquiry_form_data' || $table == 'gl_inquiry_form') { | ||
| 121 | continue; | 124 | continue; |
| 122 | } | 125 | } |
| 123 | -// DB::connection('custom_mysql')->table($table)->truncate(); // 清空目标表数据 | 126 | + DB::connection('custom_mysql')->table($table)->truncate(); // 清空目标表数据 |
| 124 | DB::connection('custom_mysql')->table($table)->insertUsing( | 127 | DB::connection('custom_mysql')->table($table)->insertUsing( |
| 125 | [], // 列名数组,留空表示插入所有列 | 128 | [], // 列名数组,留空表示插入所有列 |
| 126 | - function ($query) use ($table,$project_id) { | ||
| 127 | - $name = 'gl_data_'.$project_id.'.'.$table; | 129 | + function ($query) use ($table, $project_id) { |
| 130 | + $name = 'gl_data_' . $project_id . '.' . $table; | ||
| 128 | $query->select('*')->from("{$name}"); | 131 | $query->select('*')->from("{$name}"); |
| 129 | } | 132 | } |
| 130 | ); | 133 | ); |
| 134 | + | ||
| 131 | if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) { | 135 | if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) { |
| 132 | DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]); | 136 | DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]); |
| 133 | } | 137 | } |
-
请 注册 或 登录 后发表评论