From 2cbfd005116c28e03b9c8d62acd2022955c7f60f Mon Sep 17 00:00:00 2001 From: eddy8 Date: Mon, 20 Jan 2020 15:15:54 +0800 Subject: [PATCH] add: copy entity --- .../Controllers/Admin/EntityController.php | 33 ++++++++++++++++++ app/Repository/Admin/EntityRepository.php | 32 +++++++++++++++++ resources/views/admin/entity/index.blade.php | 34 ++++++++++++++++++- routes/admin.php | 3 ++ 4 files changed, 101 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Admin/EntityController.php b/app/Http/Controllers/Admin/EntityController.php index 37cbb70..1fb1a8d 100644 --- a/app/Http/Controllers/Admin/EntityController.php +++ b/app/Http/Controllers/Admin/EntityController.php @@ -167,4 +167,37 @@ class EntityController extends Controller 'reload' => true ]; } + + /** + * 模型管理-复制模型 + * + * @param Request $request + * @param integer $id + * @return array + */ + public function copy(Request $request, $id) + { + $this->validate($request, [ + 'table_name' => ['required', 'max:64', 'regex:/^[0-9a-zA-Z$_]+$/'], + ], [ + 'table_name.required' => '表名称不能为空', + 'table_name.max' => '表名称长度不能超过64', + 'table_name.regex' => '表名称格式有误', + ]); + + try { + $tableName = $request->post('table_name'); + EntityRepository::copy($tableName, $id); + return [ + 'code' => 0, + 'msg' => '复制成功', + 'reload' => true + ]; + } catch (\RuntimeException $e) { + return [ + 'code' => 5, + 'msg' => $e->getMessage(), + ]; + } + } } diff --git a/app/Repository/Admin/EntityRepository.php b/app/Repository/Admin/EntityRepository.php index 4fef9b0..2d88701 100644 --- a/app/Repository/Admin/EntityRepository.php +++ b/app/Repository/Admin/EntityRepository.php @@ -33,6 +33,7 @@ class EntityRepository xssFilter($item); $item->editUrl = route('admin::entity.edit', ['id' => $item->id]); $item->deleteUrl = route('admin::entity.delete', ['id' => $item->id]); + $item->copyUrl = route('admin::entity.copy', ['id' => $item->id]); $item->fieldUrl = route('admin::entityField.index') . '?entity_id=' . $item->id; $item->contentUrl = route('admin::content.index', ['entity' => $item->id]); $item->commentListUrl = route('admin::comment.index', ['entity_id' => $item->id]); @@ -80,6 +81,37 @@ class EntityRepository } } + public static function copy($tableName, $id) + { + $entity = Entity::findOrFail($id); + if (Schema::hasTable($tableName)) { + throw new \RuntimeException("数据库表已存在"); + } + + try { + // 仅在 Mysql 下测试通过,不支持 Sqlite + DB::statement("CREATE TABLE {$tableName} LIKE {$entity->table_name}"); + + DB::beginTransaction(); + + $newEntity = $entity->replicate(); + $newEntity->table_name = $tableName; + $newEntity->name .= '_copy'; + $newEntity->save(); + + EntityField::where('entity_id', $id)->get()->each(function ($item) use ($newEntity) { + $m = $item->replicate(); + $m->entity_id = $newEntity->id; + $m->save(); + }); + + DB::commit(); + } catch (\Exception $e) { + Schema::dropIfExists($tableName); + throw $e; + } + } + public static function update($id, $data) { return Entity::query()->where('id', $id)->update($data); diff --git a/resources/views/admin/entity/index.blade.php b/resources/views/admin/entity/index.blade.php index a7e3b64..f92916f 100644 --- a/resources/views/admin/entity/index.blade.php +++ b/resources/views/admin/entity/index.blade.php @@ -21,8 +21,9 @@ @endsection @@ -71,5 +72,36 @@ layer.close(index); }); } + + function copyEntity (url) { + layer.confirm('复制模型将新建一个和当前模型一样的模型(数据库表结构、表单定义等信息一致),确定要复制?', function(index){ + layer.prompt({ + formType: 0, + title: '请输入新模型的数据库表名称', + }, function(value, index, elem){ + $.ajax({ + url: url, + data: {'table_name': value}, + success: function (result) { + if (result.code !== 0) { + layer.msg(result.msg, {shift: 6}); + return false; + } + layer.msg(result.msg, {icon: 1}, function () { + if (result.reload) { + location.reload(); + } + if (result.redirect) { + location.href = '{!! url()->previous() !!}'; + } + }); + } + }); + layer.close(index); + }); + + layer.close(index); + }); + } @endsection diff --git a/routes/admin.php b/routes/admin.php index dc1093d..ed46f05 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -81,6 +81,9 @@ Route::group( // 内容 Route::post('/entity/{entity}/batch', 'ContentController@batch')->name('content.batch'); + // 模型 + Route::post('/entities/{id}/copy', 'EntityController@copy')->name('entity.copy'); + // 自动加载生成的其它路由 foreach (new DirectoryIterator(base_path('routes/auto')) as $f) { if ($f->isDot()) { -- GitLab