Skip to content

Commit

Permalink
add: copy entity
Browse files Browse the repository at this point in the history
  • Loading branch information
eddy8 committed Jan 20, 2020
1 parent a839ef8 commit 2cbfd00
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
33 changes: 33 additions & 0 deletions app/Http/Controllers/Admin/EntityController.php
Expand Up @@ -167,4 +167,37 @@ public function delete(Request $request, $id)
'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(),
];
}
}
}
32 changes: 32 additions & 0 deletions app/Repository/Admin/EntityRepository.php
Expand Up @@ -33,6 +33,7 @@ public static function list($perPage, $condition = [])
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]);
Expand Down Expand Up @@ -80,6 +81,37 @@ public static function add($data, $createDB = true)
}
}

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);
Expand Down
34 changes: 33 additions & 1 deletion resources/views/admin/entity/index.blade.php
Expand Up @@ -21,8 +21,9 @@
@endsection
<script type="text/html" id="action">
<a href="<% d.editUrl %>" class="layui-table-link" title="编辑"><i class="layui-icon layui-icon-edit"></i></a>
<%# if(d.enable_comment == {{ App\Model\Admin\Entity::COMMENT_ENABLE }}){ %> <a href="<% d.commentListUrl %>" class="layui-table-link" title="评论列表" style="margin-left: 5px"><i class="layui-icon layui-icon-reply-fill"></i></a> <%# } %>
<a href="javascript:;" class="layui-table-link" title="删除" style="margin-left: 10px" onclick="deleteEntity('<% d.deleteUrl %>')"><i class="layui-icon layui-icon-delete"></i></a>
<a href="javascript:;" class="layui-table-link" title="复制" style="margin-left: 10px" onclick="copyEntity('<% d.copyUrl %>')"><i class="layui-icon layui-icon-file"></i></a>
<%# if(d.enable_comment == {{ App\Model\Admin\Entity::COMMENT_ENABLE }}){ %> <a href="<% d.commentListUrl %>" class="layui-table-link" title="评论列表" style="margin-left: 5px"><i class="layui-icon layui-icon-reply-fill"></i></a> <%# } %>
<a href="<% d.fieldUrl %>" class="layui-table-link" title="字段管理" style="margin-left: 5px">字段管理</a>
<a href="<% d.contentUrl %>" class="layui-table-link" title="字段管理" style="margin-left: 5px">内容管理</a>
</script>
Expand Down Expand Up @@ -71,5 +72,36 @@ function deleteEntity (url) {
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);
});
}
</script>
@endsection
3 changes: 3 additions & 0 deletions routes/admin.php
Expand Up @@ -81,6 +81,9 @@ function () {
// 内容
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()) {
Expand Down

0 comments on commit 2cbfd00

Please sign in to comment.