重新定义模型
This commit is contained in:
@@ -46,19 +46,7 @@ class Attribute extends Admin {
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function index($model_id = null) {
|
||||
$map['model_id'] = $model_id;
|
||||
if (!$model_id) {
|
||||
return $this->error("非法操作!");
|
||||
}
|
||||
|
||||
$list = model('Attribute')->where($map)->order('id desc')->paginate(25);
|
||||
|
||||
$data = array(
|
||||
'list' => $list,
|
||||
'model_id' => $model_id,
|
||||
'page' => $list->render(),
|
||||
);
|
||||
$this->assign($data);
|
||||
$this->getAttributeList($model_id);
|
||||
$this->setMeta('字段管理');
|
||||
return $this->fetch();
|
||||
}
|
||||
@@ -120,13 +108,15 @@ class Attribute extends Admin {
|
||||
* @var delattr 是否删除字段表里的字段
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function del() {
|
||||
$id = input('id', '', 'trim,intval');
|
||||
public function del(\think\Request $request) {
|
||||
$id = $request->param('id');
|
||||
$model_id = $request->param('model_id');
|
||||
|
||||
if (!$id) {
|
||||
return $this->error("非法操作!");
|
||||
}
|
||||
|
||||
$result = $this->model->del($id);
|
||||
$result = $this->model->del($id, $model_id);
|
||||
if ($result) {
|
||||
return $this->success("删除成功!");
|
||||
} else {
|
||||
@@ -134,6 +124,47 @@ class Attribute extends Admin {
|
||||
}
|
||||
}
|
||||
|
||||
public function generate($id = '') {
|
||||
if ($id) {
|
||||
$model = model('Model')->where('id', $id)->find();
|
||||
$result = $this->model->generate($model);
|
||||
if (false !== $result) {
|
||||
return $this->success('生成成功!', url('admin/model/index'));
|
||||
} else {
|
||||
return $this->error($this->model->getError());
|
||||
}
|
||||
} else {
|
||||
return $this->error('非法操作!');
|
||||
}
|
||||
}
|
||||
|
||||
public function insert(\think\Request $request) {
|
||||
if (IS_POST) {
|
||||
$model_id = $request->param('id');
|
||||
$attr = db('Model')->where('id', $model_id)->value('attribute_list');
|
||||
$attr = explode(',', $attr);
|
||||
$post = $request->post();
|
||||
$attribute = array_merge($attr, $post['id']);
|
||||
|
||||
$data = array(
|
||||
'attribute_list' => implode(',', $attribute),
|
||||
'table_status' => 2,
|
||||
'status' => 0,
|
||||
);
|
||||
$result = db('Model')->where('id', $model_id)->update($data);
|
||||
if (false !== $result) {
|
||||
return $this->success('成功导入!');
|
||||
}else{
|
||||
return $this->error('导入失败!');
|
||||
}
|
||||
} else {
|
||||
$this->getAttributeList();
|
||||
$this->assign('id', $request->param('id'));
|
||||
$this->setMeta('导入字段');
|
||||
return $this->fetch();
|
||||
}
|
||||
}
|
||||
|
||||
//字段编辑所需字段
|
||||
protected function getField() {
|
||||
return array(
|
||||
@@ -161,4 +192,21 @@ class Attribute extends Admin {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
protected function getAttributeList($model_id = '') {
|
||||
$map = array();
|
||||
if ($model_id) {
|
||||
$attribute = db('Model')->where('id', $model_id)->value('attribute_list');
|
||||
$map['id'] = array('IN', $attribute);
|
||||
}
|
||||
|
||||
$list = model('Attribute')->where($map)->order('id desc')->paginate(25);
|
||||
|
||||
$data = array(
|
||||
'list' => $list,
|
||||
'model_id' => $model_id,
|
||||
'page' => $list->render(),
|
||||
);
|
||||
$this->assign($data);
|
||||
}
|
||||
}
|
||||
@@ -16,20 +16,13 @@ class Content extends Admin {
|
||||
parent::_initialize();
|
||||
$this->getContentMenu();
|
||||
$this->model_id = $model_id = $this->request->param('model_id');
|
||||
$row = db('Model')->select();
|
||||
foreach ($row as $key => $value) {
|
||||
$list[$value['id']] = $value;
|
||||
}
|
||||
$list = db('Model')->column('*', 'id');
|
||||
|
||||
if (empty($list[$model_id])) {
|
||||
return $this->error("无此模型!");
|
||||
} else {
|
||||
$this->modelInfo = $list[$model_id];
|
||||
if ($this->modelInfo['extend'] > 1) {
|
||||
$this->model = model('Content')->extend($this->modelInfo['name']);
|
||||
} else {
|
||||
$this->model = model('Document')->extend($this->modelInfo['name']);
|
||||
}
|
||||
$this->model = model('Content')->extend($this->modelInfo['name']);
|
||||
}
|
||||
|
||||
$this->assign('model_id', $model_id);
|
||||
@@ -49,11 +42,7 @@ class Content extends Admin {
|
||||
$order = "id desc";
|
||||
$map = $this->buildMap();
|
||||
$field = array_filter($grid_list['fields']);
|
||||
if ($this->modelInfo['extend'] == 1) {
|
||||
array_push($field, 'is_top');
|
||||
} else {
|
||||
unset($map['model_id']);
|
||||
}
|
||||
|
||||
|
||||
$list = $this->model->lists($map, $order);
|
||||
|
||||
@@ -247,7 +236,7 @@ class Content extends Admin {
|
||||
*/
|
||||
protected function buildMap() {
|
||||
$map = array();
|
||||
$data = $this->request->get();
|
||||
$data = $this->request->param();
|
||||
foreach ($data as $key => $value) {
|
||||
if ($value) {
|
||||
if ($key == 'keyword') {
|
||||
@@ -264,12 +253,6 @@ class Content extends Admin {
|
||||
if (isset($map['page'])) {
|
||||
unset($map['page']);
|
||||
}
|
||||
if ($this->modelInfo['extend'] == 1) {
|
||||
$category = isset($data['category']) ? $data['category'] : '';
|
||||
$cate_list = parse_field_bind('category', $category, 0);
|
||||
$map['model_id'] = $this->model_id;
|
||||
$this->assign('cate_list', $cate_list);
|
||||
}
|
||||
$this->assign($data);
|
||||
return $map;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ class Model extends Admin {
|
||||
public function _initialize() {
|
||||
parent::_initialize();
|
||||
$this->getContentMenu();
|
||||
$this->model = model('Model');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -25,7 +26,7 @@ class Model extends Admin {
|
||||
$map = array('status' => array('gt', -1));
|
||||
|
||||
$order = "id desc";
|
||||
$list = model('Model')->where($map)->order($order)->paginate(10);
|
||||
$list = $this->model->where($map)->order($order)->paginate(10);
|
||||
|
||||
$data = array(
|
||||
'list' => $list,
|
||||
@@ -44,75 +45,73 @@ class Model extends Admin {
|
||||
* 新增页面初始化
|
||||
* @author huajie <banhuajie@163.com>
|
||||
*/
|
||||
public function add() {
|
||||
|
||||
//获取所有的模型
|
||||
$models = db('Model')->where(array('extend' => 0))->field('id,title')->select();
|
||||
|
||||
$this->assign('models', $models);
|
||||
$this->setMeta('新增模型');
|
||||
return $this->fetch();
|
||||
public function add(\think\Request $request) {
|
||||
if (IS_POST) {
|
||||
$result = $this->model->validate('Model.add')->save($request->post());
|
||||
if (false !== $result) {
|
||||
$this->success('创建成功!', url('admin/model/index'));
|
||||
}else{
|
||||
return $this->error($this->model->getError());
|
||||
}
|
||||
}else{
|
||||
$this->setMeta('新增模型');
|
||||
return $this->fetch();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑页面初始化
|
||||
* @author huajie <banhuajie@163.com>
|
||||
* @author molong <molong@tensent.cn>
|
||||
*/
|
||||
public function edit() {
|
||||
$id = input('id', '', 'trim,intval');
|
||||
if (empty($id)) {
|
||||
return $this->error('参数不能为空!');
|
||||
}
|
||||
|
||||
/*获取一条记录的详细数据*/
|
||||
$model = model('Model');
|
||||
$data = $model::find($id);
|
||||
if (!$data) {
|
||||
return $this->error($Model->getError());
|
||||
}
|
||||
$data['attribute_list'] = empty($data['attribute_list']) ? '' : explode(",", $data['attribute_list']);
|
||||
|
||||
// 是否继承了其他模型
|
||||
if ($data['extend'] == 1) {
|
||||
$map['model_id'] = array('IN', array($data['id'], $data['extend']));
|
||||
} else {
|
||||
$map['model_id'] = $data['id'];
|
||||
}
|
||||
$map['is_show'] = 1;
|
||||
$fields = db('Attribute')->where($map)->select();
|
||||
|
||||
// 梳理属性的可见性
|
||||
foreach ($fields as $key => $field) {
|
||||
if (!empty($data['attribute_list']) && !in_array($field['id'], $data['attribute_list'])) {
|
||||
$field['is_show'] = 0;
|
||||
public function edit(\think\Request $request) {
|
||||
if (IS_POST) {
|
||||
$result = $this->model->validate('Model.edit')->save($request->post(), array('id'=>$request->post('id')));
|
||||
if (false !== $result) {
|
||||
$this->success('更新成功!', url('admin/model/index'));
|
||||
}else{
|
||||
return $this->error($this->model->getError());
|
||||
}
|
||||
$field['group'] = -1;
|
||||
$field['sort'] = 0;
|
||||
$fields_tem[$field['id']] = $field;
|
||||
}
|
||||
}else{
|
||||
$info = $this->model->where('id', $request->param('id'))->find();
|
||||
|
||||
// 获取模型排序字段
|
||||
$field_sort = json_decode($data['field_sort'], true);
|
||||
if (!empty($field_sort)) {
|
||||
foreach ($field_sort as $group => $ids) {
|
||||
foreach ($ids as $key => $value) {
|
||||
if (!empty($fields_tem[$value])) {
|
||||
$fields_tem[$value]['group'] = $group;
|
||||
$fields_tem[$value]['sort'] = $key;
|
||||
//获取字段列表
|
||||
if ($info['attribute_list']) {
|
||||
$fields = model('Attribute')->where('id', 'IN', $info['attribute_list'])->where('is_show', 1)->select();
|
||||
// 梳理属性的可见性
|
||||
foreach ($fields as $key => $field) {
|
||||
$field['group'] = -1;
|
||||
$field['sort'] = 0;
|
||||
$fields_tem[$field['id']] = $field->toArray(); //数据对象转换为数组
|
||||
}
|
||||
|
||||
// 获取模型排序字段
|
||||
$field_sort = json_decode($info['attribute_sort'], true);//dump($field_sort);exit();
|
||||
if (!empty($field_sort)) {
|
||||
foreach ($field_sort as $group => $ids) {
|
||||
foreach ($ids as $key => $value) {
|
||||
if (!empty($fields_tem[$value])) {
|
||||
$fields_tem[$value]['group'] = $group;
|
||||
$fields_tem[$value]['sort'] = $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($fields_tem) && $fields_tem) {
|
||||
// 模型字段列表排序
|
||||
$fields = list_sort_by($fields_tem, "sort");
|
||||
}
|
||||
}else{
|
||||
$fields = array();
|
||||
}
|
||||
$data = array(
|
||||
'info' => $info,
|
||||
'fields' => $fields
|
||||
);
|
||||
$this->assign($data);
|
||||
$this->setMeta('编辑模型');
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
if (isset($fields_tem) && $fields_tem) {
|
||||
// 模型字段列表排序
|
||||
$fields = list_sort_by($fields_tem, "sort");
|
||||
}
|
||||
|
||||
$this->assign('fields', $fields);
|
||||
$this->assign('info', $data);
|
||||
$this->setMeta('编辑模型');
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,12 +119,11 @@ class Model extends Admin {
|
||||
* @author huajie <banhuajie@163.com>
|
||||
*/
|
||||
public function del() {
|
||||
$mdoel = model('Model');
|
||||
$result = $mdoel->del();
|
||||
$result = $this->model->del();
|
||||
if ($result) {
|
||||
return $this->success('删除模型成功!');
|
||||
} else {
|
||||
return $this->error($mdoel->getError());
|
||||
return $this->error($this->mdoel->getError());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,24 +144,27 @@ class Model extends Admin {
|
||||
* 更新数据
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function status() {
|
||||
$map['id'] = $this->request->param('ids');
|
||||
if (null == $map['id']) {
|
||||
public function status(\think\Request $request) {
|
||||
$map['id'] = $request->param('id');
|
||||
|
||||
$data['status'] = $request->param('status');
|
||||
|
||||
if (null == $map['id'] || null == $data['status']) {
|
||||
return $this->error('参数不正确!');
|
||||
}
|
||||
|
||||
$data['status'] = input('get.status');
|
||||
|
||||
if (null == $data['status']) {
|
||||
//实现单条数据数据修改
|
||||
$status = db('Model')->where($map)->field('status')->find();
|
||||
$data['status'] = $status['status'] ? 0 : 1;
|
||||
db('Model')->where($map)->update($data);
|
||||
} else {
|
||||
//实现多条数据同时修改
|
||||
$map['id'] = array('IN', $map['id']);
|
||||
db('Model')->where($map)->update($data);
|
||||
$model = $this->model->where($map)->find();
|
||||
if ($model['table_status'] != 1 && $data['status'] == 1) {
|
||||
return $this->error('数据表未创建或更新');
|
||||
}
|
||||
if ($model['list_grid'] == '' && $data['status'] == 1) {
|
||||
return $this->error('模型列表未定义');
|
||||
}
|
||||
$result = $this->model->where($map)->update($data);
|
||||
if (false !== $result) {
|
||||
return $this->success('状态设置成功!');
|
||||
}else{
|
||||
return $this->error($this->model->getError());
|
||||
}
|
||||
return $this->success('状态设置成功!');
|
||||
}
|
||||
}
|
||||
@@ -7270,4 +7270,6 @@ to {
|
||||
padding-left:48px;
|
||||
}
|
||||
}
|
||||
span.reloadverify{padding: 0; overflow: hidden; cursor: pointer;}
|
||||
span.reloadverify{padding: 0; overflow: hidden; cursor: pointer;}
|
||||
.footer-bar{position: fixed; bottom: 0; left: 0; right: 0; background: #fff; overflow: hidden; padding: 10px; text-align: right; z-index: 10000;}
|
||||
.dialog-body{padding: 10px; padding-bottom: 60px;}
|
||||
@@ -6,9 +6,13 @@
|
||||
<h2>{$meta_title}</h2>
|
||||
</div>
|
||||
<div class="pull-right">
|
||||
<a class="btn btn-info" href="{:url('Model/index')}">返回</a>
|
||||
<a class="btn btn-primary" href="{:url('add',array('model_id'=>$model_id))}">新 增</a>
|
||||
<button class="btn btn-danger ajax-post confirm" url="{:url('del')}" target-form="ids">删 除</button>
|
||||
<a class="btn btn-danger" href="{:url('add',array('model_id'=>$model_id))}"><i class="fa fa-plus"></i> 新 增</a>
|
||||
{if $model_id}
|
||||
<button class="btn btn-primary openDilog" url="{:url('insert?id='.$model_id)}">导入字段</button>
|
||||
<button class="btn btn-primary ajax-get" url="{:url('generate?id='.$model_id)}">生成数据库</button>
|
||||
{/if}
|
||||
<!-- <button class="btn btn-danger ajax-post confirm" url="{:url('del')}" target-form="ids"><i class="fa fa-remove"></i> 删 除</button> -->
|
||||
<a class="btn btn-warning" href="{:url('Model/index')}"><i class="fa fa-reply"></i> 返回</a>
|
||||
</div>
|
||||
</header>
|
||||
<div class="main-box-body clearfix">
|
||||
@@ -36,7 +40,7 @@
|
||||
<td>{$item['value']}</td>
|
||||
<td>
|
||||
<a href="{:url('edit',array('id'=>$item['id']))}">编辑</a>
|
||||
<a href="{:url('del',array('id'=>$item['id']))}" class="confirm ajax-get">删除</a>
|
||||
<a href="{:url('del',array('id'=>$item['id'], 'model_id'=>$model_id))}" class="confirm ajax-get">删除</a>
|
||||
</td>
|
||||
</tr>
|
||||
{/volist}
|
||||
@@ -46,4 +50,19 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script type="text/javascript" src="__PUBLIC__/plugs/layer/layer.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(function(){
|
||||
$('.openDilog').click(function(){
|
||||
layer.open({
|
||||
type: 2,
|
||||
title: false,
|
||||
area: ['900px', '560px'],
|
||||
content: $('.openDilog').attr('url')
|
||||
});
|
||||
});
|
||||
})
|
||||
</script>
|
||||
{/block}
|
||||
60
application/admin/view/attribute/insert.html
Normal file
60
application/admin/view/attribute/insert.html
Normal file
@@ -0,0 +1,60 @@
|
||||
{include file="admin@public/header" /}
|
||||
<div class="dialog-body">
|
||||
<div class="main-box clearfix">
|
||||
<header class="main-box-header clearfix">
|
||||
<div class="pull-left">
|
||||
<h2>{$meta_title}</h2>
|
||||
</div>
|
||||
</header>
|
||||
<div class="main-box-body clearfix">
|
||||
<div class="table-responsive clearfix">
|
||||
<table class="table table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><input class="checkbox check-all" type="checkbox"></th>
|
||||
<th>表单标题</th>
|
||||
<th>字段名</th>
|
||||
<th>字段类型</th>
|
||||
<th>字段长度</th>
|
||||
<th>默认值</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{volist name="list" id="item"}
|
||||
<tr>
|
||||
<td><input class="ids row-selected" type="checkbox" name="id[]" value="{$item['id']}"></td>
|
||||
<td>{$item['title']}</td>
|
||||
<td>{$item['name']}</td>
|
||||
<td>{$item['type_text']}</td>
|
||||
<td>{$item['length']}</td>
|
||||
<td>{$item['value']}</td>
|
||||
</tr>
|
||||
{/volist}
|
||||
</tbody>
|
||||
</table>
|
||||
{$page}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="footer-bar">
|
||||
<button class="btn btn-primary insert-btn">确认导入</button>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
|
||||
//关闭iframe
|
||||
$('button.insert-btn').click(function(){
|
||||
$.ajax({
|
||||
url : "{:url('admin/attribute/insert?id='.$id)}",
|
||||
data : $('input.ids:checked').serialize(),
|
||||
type : "POST",
|
||||
success : function(res){
|
||||
if (res.code == 1) {
|
||||
parent.layer.close(index);
|
||||
}
|
||||
},
|
||||
dataType : 'json'
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{include file="admin@public/footer" /}
|
||||
@@ -12,7 +12,7 @@
|
||||
<div class="main-box-body clearfix">
|
||||
|
||||
<!-- 表单 -->
|
||||
<form id="form" action="{:url('update')}" method="post" class="form-horizontal form">
|
||||
<form id="form" method="post" class="form-horizontal form">
|
||||
<!-- 基础 -->
|
||||
<div id="tab1" class="tab-pane in tab1">
|
||||
<div class="form-group">
|
||||
@@ -29,50 +29,6 @@
|
||||
<span class="help-block">(请输入模型的名称)</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">模型类型</label>
|
||||
<div class="col-lg-3 col-sm-10">
|
||||
<select name="extend" class="form-control" style="width:auto">
|
||||
<option value="2">独立模型</option>
|
||||
<option value="1" selected="selected">文档模型</option>
|
||||
</select>
|
||||
<span class="help-block">(目前只支持独立模型和文档模型)</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">引擎类型</label>
|
||||
<div class="col-lg-3 col-sm-10">
|
||||
<select name="engine_type" class="form-control" style="width:auto">
|
||||
<option value="MyISAM">MyISAM</option>
|
||||
<option value="InnoDB">InnoDB</option>
|
||||
<option value="MEMORY">MEMORY</option>
|
||||
<option value="BLACKHOLE">BLACKHOLE</option>
|
||||
<option value="MRG_MYISAM">MRG_MYISAM</option>
|
||||
<option value="ARCHIVE">ARCHIVE</option>
|
||||
</select>
|
||||
<span class="help-block"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">是否需要主键</label>
|
||||
<div class="col-lg-10">
|
||||
<select name="need_pk" class="form-control" style="width:auto">
|
||||
<option value="1" selected="selected">是</option>
|
||||
<option value="0">否</option>
|
||||
</select>
|
||||
<span class="help-block">(选“是”则会新建默认的id字段作为主键)</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">是否会员显示</label>
|
||||
<div class="col-lg-10">
|
||||
<select name="is_user_show" class="form-control" style="width:auto">
|
||||
<option value="1" selected="selected">是</option>
|
||||
<option value="0">否</option>
|
||||
</select>
|
||||
<span class="help-block">(选“是”则会新建默认的id字段作为主键)</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">模型图标</label>
|
||||
<div class="col-lg-6 col-sm-10">
|
||||
@@ -85,7 +41,6 @@
|
||||
<!-- 按钮 -->
|
||||
<div class="form-group">
|
||||
<div class="col-lg-offset-2 col-lg-10">
|
||||
<input type="hidden" name="id" value="{$info.id|default=''}">
|
||||
<button class="btn btn-success submit-btn ajax-post" type="submit" target-form="form-horizontal">确 定</button>
|
||||
<button class="btn btn-danger btn-return" onclick="javascript:history.back(-1);return false;">返 回</button>
|
||||
</div>
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
<div class="main-box clearfix">
|
||||
<header class="main-box-header clearfix">
|
||||
<div class="pull-left">
|
||||
<h2>{if condition="ACTION_NAME eq 'add'"}新增{else/}编辑{/if}模型</h2>
|
||||
<h2>编辑模型</h2>
|
||||
</div>
|
||||
<div class="pull-right">
|
||||
</div>
|
||||
</header>
|
||||
<div class="main-box-body clearfix">
|
||||
<form id="form" action="{:url('update')}" method="post" class="form form-horizontal">
|
||||
<form id="form" method="post" class="form form-horizontal">
|
||||
<div class="tabs-wrapper">
|
||||
<ul class="nav nav-tabs">
|
||||
<li class="active"><a href="#tab1" data-toggle="tab">基 础</a></li>
|
||||
@@ -25,7 +25,7 @@
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">模型标识</label>
|
||||
<div class="col-lg-6 col-sm-10">
|
||||
<input type="text" class="form-control disabled" name="name" value="{$info.name}" disabled>
|
||||
<input type="text" class="form-control disabled" name="name" value="{$info['name']}" disabled>
|
||||
<span class="help-block">(请输入文档模型标识)</span>
|
||||
</div>
|
||||
</div>
|
||||
@@ -36,27 +36,6 @@
|
||||
<span class="help-block">(请输入模型的名称)</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">模型类型</label>
|
||||
<div class="col-lg-3 col-sm-10">
|
||||
<select name="extend" class="form-control disabled" readonly disabled>
|
||||
<option value="0" {if condition="$info['extend'] eq '0'"}selected{/if}>--请选择--</option>
|
||||
<option value="1" {if condition="$info['extend'] eq '1'"}selected{/if}>文档模型</option>
|
||||
<option value="2" {if condition="$info['extend'] eq '2'"}selected{/if}>独立模型</option>
|
||||
</select>
|
||||
<span class="help-block">(目前只支持独立模型和文档模型)</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">是否会员显示</label>
|
||||
<div class="col-lg-10">
|
||||
<select name="is_user_show" class="form-control" style="width:auto">
|
||||
<option value="1" {if $info['is_user_show'] ==1}selected="selected"{/if}>是</option>
|
||||
<option value="0" {if $info['is_user_show'] ==0}selected="selected"{/if}>否</option>
|
||||
</select>
|
||||
<span class="help-block">(选“是”则会新建默认的id字段作为主键)</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">模型图标</label>
|
||||
<div class="col-lg-6 col-sm-10">
|
||||
@@ -70,21 +49,21 @@
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">字段别名定义</label>
|
||||
<div class="col-lg-6 col-sm-10">
|
||||
<textarea name="attribute_alias" class="form-control">{$info.attribute_alias}</textarea>
|
||||
<textarea name="attribute_alias" class="form-control">{$info['attribute_alias']}</textarea>
|
||||
<span class="help-block">(用于表单显示的名称)</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">表单显示分组</label>
|
||||
<div class="col-lg-6 col-sm-10">
|
||||
<input type="text" class="form-control" name="field_group" value="{$info.field_group}">
|
||||
<input type="text" class="form-control" name="attribute_group" value="{$info['attribute_group']}">
|
||||
<span class="help-block">(用于表单显示的分组,以及设置该模型表单排序的显示)</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">表单显示排序</label>
|
||||
<div class="col-lg-10 boards" id="field_group_sort">
|
||||
{volist name=":parse_field_attr($info['field_group'])" id="vo"}
|
||||
<div class="col-lg-10 boards" id="attribute_group_sort">
|
||||
{volist name=":parse_field_attr($info['attribute_group'])" id="vo"}
|
||||
<div class="board panel panel-info">
|
||||
<div class="panel-heading">{$vo}</div>
|
||||
<div class="panel-body">
|
||||
@@ -93,7 +72,7 @@
|
||||
{if (($field['group'] == -1) or ($field['group'] eq $key))}
|
||||
<div class="board-item">
|
||||
<span data="{$field['id']}">{$field['title']} [{$field['name']}]</span>
|
||||
<input type="hidden" name="field_sort[{$key}][]" value="{$field['id']}"/>
|
||||
<input type="hidden" name="attribute_sort[{$key}][]" value="{$field['id']}"/>
|
||||
</div>
|
||||
{php}
|
||||
unset($fields[$k]);
|
||||
@@ -112,7 +91,7 @@
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">列表定义</label>
|
||||
<div class="col-lg-6 col-sm-10">
|
||||
<textarea name="list_grid" class="form-control">{$info.list_grid}</textarea>
|
||||
<textarea name="list_grid" class="form-control">{$info['list_grid']}</textarea>
|
||||
<span class="help-block">(默认列表模板的展示规则)</span>
|
||||
</div>
|
||||
</div>
|
||||
@@ -120,14 +99,14 @@
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">默认搜索字段</label>
|
||||
<div class="col-lg-6 col-sm-10">
|
||||
<input type="text" class="form-control" name="search_key" value="{$info.search_key}">
|
||||
<input type="text" class="form-control" name="search_key" value="{$info['search_key']}">
|
||||
<span class="help-block">(默认列表模板的默认搜索项)</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">高级搜索字段</label>
|
||||
<div class="col-lg-6 col-sm-10">
|
||||
<input type="text" class="form-control" name="search_list" value="{$info.search_list}">
|
||||
<input type="text" class="form-control" name="search_list" value="{$info['search_list']}">
|
||||
<span class="help-block">(默认列表模板的高级搜索项)</span>
|
||||
</div>
|
||||
</div>
|
||||
@@ -180,11 +159,11 @@
|
||||
<script type="text/javascript" src="__PUBLIC__/plugs/board/board.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(function(){
|
||||
$('.form-group #field_sort').boards();
|
||||
$('.form-group #field_group_sort').boards({
|
||||
$('.form-group #attribute_sort').boards();
|
||||
$('.form-group #attribute_group_sort').boards({
|
||||
drop: function(e){
|
||||
var group = e.target.closest('.board').find('.board-list').attr('data-group');
|
||||
e.element.find('input').attr('name','field_sort[' + group + '][]')
|
||||
e.element.find('input').attr('name','attribute_sort[' + group + '][]')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
</div>
|
||||
<div class="pull-right">
|
||||
<a class="btn btn-success" href="{:url('Model/add')}">新 增</a>
|
||||
<button class="btn btn-info ajax-post" target-form="ids" url="{:url('Model/status',array('status'=>1))}">启 用</button>
|
||||
<button class="btn ajax-post" target-form="ids" url="{:url('Model/status',array('status'=>0))}">禁 用</button>
|
||||
</div>
|
||||
</header>
|
||||
@@ -36,7 +35,7 @@
|
||||
<input class="ids" type="checkbox" name="ids[]" value="{$item['id']}" />
|
||||
</td>
|
||||
<td>{$item['id']}</td>
|
||||
<td>{$item['name']}</td>
|
||||
<td><i class="fa fa-{$item['icon']}"></i> {$item['name']}</td>
|
||||
<td>
|
||||
<a data-id="{$item.id}" href="{:url('model/edit?id='.$item['id'])}">{$item.title}</a>
|
||||
</td>
|
||||
@@ -52,17 +51,15 @@
|
||||
</td>
|
||||
<td>
|
||||
<a href="{:url('admin/attribute/index?model_id='.$item['id'])}">字段</a>
|
||||
<a href="{:url('model/status?ids='.$item['id'].'&status='.abs(1-$item['status']))}" class="ajax-get">{$item['status']|show_status_op}</a>
|
||||
<a href="{:url('model/status?id='.$item['id'].'&status='.abs(1-$item['status']))}" class="ajax-get">{$item['status']|show_status_op}</a>
|
||||
<a href="{:url('model/edit?id='.$item['id'])}">编辑</a>
|
||||
<a href="{:url('model/del?id='.$item['id'])}" class="confirm ajax-get">删除</a>
|
||||
{if $item['extend']}
|
||||
<a href="{:url('admin/content/index?model_id='.$item['id'])}">数据</a>
|
||||
{/if}
|
||||
</td>
|
||||
</tr>
|
||||
{/volist}
|
||||
{else/}
|
||||
<td colspan="7" class="text-center">aOh! 暂时还没有内容!</td>
|
||||
<td colspan="7" class="text-center">aOh! 暂时还没有创建模型!</td>
|
||||
{/notempty}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -1,28 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
|
||||
<title>SentCMS网站管理系统后台</title>
|
||||
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/bootstrap/bootstrap.min.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/libs/font-awesome.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/libs/nanoscroller.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/common.css"/>
|
||||
<!-- <link href='//fonts.googleapis.com/css?family=Open+Sans:400,600,700,300|Titillium+Web:200,300,400' rel='stylesheet' type='text/css'> -->
|
||||
<link rel="stylesheet" type="text/css" href="__CSS__/style.css"/>
|
||||
<script src="__PUBLIC__/js/jquery.js"></script>
|
||||
<script type="text/javascript">
|
||||
var BASE_URL = "{:config('base_url')}"; //根目录地址
|
||||
</script>
|
||||
<!--[if lt IE 9]>
|
||||
<script src="__PUBLIC__/js/html5shiv.js"></script>
|
||||
<script src="__PUBLIC__/js/respond.min.js"></script>
|
||||
<![endif]-->
|
||||
{include file="admin@public/header" /}
|
||||
{block name="style"}{/block}
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div id="theme-wrapper">
|
||||
<header class="navbar" id="header-navbar">
|
||||
<div class="container">
|
||||
@@ -232,28 +209,5 @@
|
||||
</div>
|
||||
</div>
|
||||
{include file="admin@public/tool" /}
|
||||
<script src="__PUBLIC__/js/bootstrap.js"></script>
|
||||
<script src="__PUBLIC__/js/jquery.nanoscroller.min.js"></script>
|
||||
<script src="__PUBLIC__/js/pace.min.js"></script>
|
||||
|
||||
<script src="__PUBLIC__/js/hopscotch.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/libs/hopscotch.css">
|
||||
|
||||
<script src="__PUBLIC__/js/messager.js"></script>
|
||||
<script src="__JS__/app.js"></script>
|
||||
<script type="text/javascript">
|
||||
(function(){
|
||||
var SentCMS = window.Sent = {
|
||||
"ROOT" : "__ROOT__", //当前网站地址
|
||||
"APP" : "__APP__", //当前项目地址
|
||||
"PUBLIC" : "__PUBLIC__", //项目公共目录地址
|
||||
"DEEP" : "{:config('URL_PATHINFO_DEPR')}", //PATHINFO分割符
|
||||
"MODEL" : ["{:config('URL_MODEL')}", "{:config('URL_CASE_INSENSITIVE')}", "{:config('URL_HTML_SUFFIX')}"],
|
||||
"VAR" : ["{:config('VAR_MODULE')}", "{:config('VAR_CONTROLLER')}", "{:config('VAR_ACTION')}"]
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
<script src="__PUBLIC__/js/core.js"></script>
|
||||
{block name="script"}{/block}
|
||||
</body>
|
||||
</html>
|
||||
{include file="admin@public/footer" /}
|
||||
{block name="script"}{/block}
|
||||
24
application/admin/view/public/footer.html
Normal file
24
application/admin/view/public/footer.html
Normal file
@@ -0,0 +1,24 @@
|
||||
<script src="__PUBLIC__/js/bootstrap.js"></script>
|
||||
<script src="__PUBLIC__/js/jquery.nanoscroller.min.js"></script>
|
||||
<script src="__PUBLIC__/js/pace.min.js"></script>
|
||||
|
||||
<script src="__PUBLIC__/js/hopscotch.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/libs/hopscotch.css">
|
||||
|
||||
<script src="__PUBLIC__/js/messager.js"></script>
|
||||
<script src="__JS__/app.js"></script>
|
||||
<script type="text/javascript">
|
||||
(function(){
|
||||
var SentCMS = window.Sent = {
|
||||
"ROOT" : "__ROOT__", //当前网站地址
|
||||
"APP" : "__APP__", //当前项目地址
|
||||
"PUBLIC" : "__PUBLIC__", //项目公共目录地址
|
||||
"DEEP" : "{:config('URL_PATHINFO_DEPR')}", //PATHINFO分割符
|
||||
"MODEL" : ["{:config('URL_MODEL')}", "{:config('URL_CASE_INSENSITIVE')}", "{:config('URL_HTML_SUFFIX')}"],
|
||||
"VAR" : ["{:config('VAR_MODULE')}", "{:config('VAR_CONTROLLER')}", "{:config('VAR_ACTION')}"]
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
<script src="__PUBLIC__/js/core.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
23
application/admin/view/public/header.html
Normal file
23
application/admin/view/public/header.html
Normal file
@@ -0,0 +1,23 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
|
||||
<title>SentCMS网站管理系统后台</title>
|
||||
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/bootstrap/bootstrap.min.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/libs/font-awesome.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/libs/nanoscroller.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/common.css"/>
|
||||
<!-- <link href='//fonts.googleapis.com/css?family=Open+Sans:400,600,700,300|Titillium+Web:200,300,400' rel='stylesheet' type='text/css'> -->
|
||||
<link rel="stylesheet" type="text/css" href="__CSS__/style.css"/>
|
||||
<script src="__PUBLIC__/js/jquery.js"></script>
|
||||
<script type="text/javascript">
|
||||
var BASE_URL = "{:config('base_url')}"; //根目录地址
|
||||
</script>
|
||||
<!--[if lt IE 9]>
|
||||
<script src="__PUBLIC__/js/html5shiv.js"></script>
|
||||
<script src="__PUBLIC__/js/respond.min.js"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
<body>
|
||||
@@ -8,7 +8,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
// SentCMS常量定义
|
||||
define('SENTCMS_VERSION', '3.0.20161201');
|
||||
define('SENTCMS_VERSION', '3.1.201706');
|
||||
define('SENT_ADDON_PATH', ROOT_PATH . DS . 'addons' . DS);
|
||||
|
||||
//字符串解密加密
|
||||
|
||||
@@ -51,12 +51,7 @@ class InitHook {
|
||||
foreach ($list as $key => $value) {
|
||||
$route[$value['rule']] = $value['url'];
|
||||
}
|
||||
$model = db('Model');
|
||||
$map = array(
|
||||
'status' => array('gt', 0),
|
||||
'extend' => array('gt', 0),
|
||||
);
|
||||
$list = $model->where($map)->field("name,id,title,'' as 'style'")->select();
|
||||
$list = db('Model')->field("name,id")->select();
|
||||
foreach ($list as $key => $value) {
|
||||
$route["admin/" . $value['name'] . "/index"] = "admin/content/index?model_id=" . $value['id'];
|
||||
$route["admin/" . $value['name'] . "/add"] = "admin/content/add?model_id=" . $value['id'];
|
||||
|
||||
@@ -174,8 +174,7 @@ class Admin extends Base {
|
||||
$model = \think\Loader::model('Model');
|
||||
$list = array();
|
||||
$map = array(
|
||||
'status' => array('gt', 0),
|
||||
'extend' => array('gt', 0),
|
||||
'status' => array('gt', 0)
|
||||
);
|
||||
$list = $model::where($map)->field("name,id,title,icon,'' as 'style'")->select();
|
||||
|
||||
|
||||
@@ -65,23 +65,21 @@ class Attribute extends Base{
|
||||
}
|
||||
}
|
||||
|
||||
public function del($id){
|
||||
public function del($id, $model_id){
|
||||
$map['id'] = $id;
|
||||
$info = $this->find($id);
|
||||
$model = db('Model')->where(array('id'=>$info['model_id']))->find();
|
||||
$tablename = db('Model')->where(array('id'=>$model_id))->value('name');
|
||||
|
||||
//先删除字段表内的数据
|
||||
$result = $this->where($map)->delete();
|
||||
if ($result) {
|
||||
if ($model['extend'] == 1) {
|
||||
$tablename = 'document_'.$model['name'];
|
||||
}else{
|
||||
$tablename = $model['name'];
|
||||
}
|
||||
|
||||
$tablename = strtolower($tablename);
|
||||
//删除模型表中字段
|
||||
$db = new \com\Datatable();
|
||||
$result = $db->del_field($tablename,$info['name'])->query();
|
||||
if (!$db->CheckField($tablename,$info['name'])) {
|
||||
return true;
|
||||
}
|
||||
$result = $db->delField($tablename,$info['name'])->query();
|
||||
if ($result) {
|
||||
return true;
|
||||
}else{
|
||||
@@ -134,4 +132,15 @@ class Attribute extends Base{
|
||||
$result = $db->create();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function generate($model){
|
||||
$tablename = strtolower($model['name']);
|
||||
//实例化一个数据库操作类
|
||||
$db = new \com\Datatable();
|
||||
//检查表是否存在并创建
|
||||
if (!$db->CheckTable($tablename)) {
|
||||
//创建新表
|
||||
$db->initTable($tablename, $model['title'], 'id')->query();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -10,34 +10,30 @@
|
||||
namespace app\common\model;
|
||||
|
||||
/**
|
||||
* 设置模型
|
||||
*/
|
||||
class Model extends Base{
|
||||
* 设置模型
|
||||
*/
|
||||
class Model extends Base {
|
||||
|
||||
protected $auto = [ 'update_time'];
|
||||
protected $insert = ['name', 'create_time', 'status'=>1];
|
||||
protected $type = array(
|
||||
'id' => 'integer',
|
||||
'create_time' => 'integer',
|
||||
'update_time' => 'integer'
|
||||
protected $auto = ['update_time'];
|
||||
protected $insert = ['name', 'create_time', 'status' => 0];
|
||||
protected $type = array(
|
||||
'id' => 'integer',
|
||||
'create_time' => 'integer',
|
||||
'update_time' => 'integer',
|
||||
);
|
||||
|
||||
public function setFieldSortAttr($value){
|
||||
return empty($value) ? '' : json_encode($value);
|
||||
protected function setAttributeSortAttr($value){
|
||||
return $value ? json_encode($value) : '';
|
||||
}
|
||||
|
||||
public function setNameAttr($value){
|
||||
public function setNameAttr($value) {
|
||||
return strtolower($value);
|
||||
}
|
||||
|
||||
public function setAttributeListAttr($value){
|
||||
return empty($value) ? '' : json_encode($value);
|
||||
}
|
||||
|
||||
public function getStatusTextAttr($value, $data){
|
||||
public function getStatusTextAttr($value, $data) {
|
||||
$status = array(
|
||||
0 => '禁用',
|
||||
1 => '启用',
|
||||
0 => '禁用',
|
||||
1 => '启用',
|
||||
);
|
||||
return $status[$data['status']];
|
||||
}
|
||||
@@ -47,67 +43,59 @@ class Model extends Base{
|
||||
* @return array
|
||||
*/
|
||||
public function change() {
|
||||
if(IS_POST){
|
||||
if (IS_POST) {
|
||||
$data = \think\Request::instance()->post();
|
||||
if($data){
|
||||
if ($data) {
|
||||
if (empty($data['id'])) {
|
||||
/*创建表*/
|
||||
$db = new \com\Datatable();
|
||||
|
||||
if ($data['extend'] == 1) {
|
||||
//文档模型
|
||||
$sql = $db->start_table('document_'.$data['name'])->create_id('doc_id', 11 , '主键' , false)->create_key('doc_id');
|
||||
}else{
|
||||
$sql = $db->start_table($data['name'])->create_id('id', 11 , '主键' , true)->create_uid()->create_key('id');
|
||||
$sql = $db->start_table('document_' . $data['name'])->create_id('doc_id', 11, '主键', false)->create_key('doc_id');
|
||||
} else {
|
||||
$sql = $db->start_table($data['name'])->create_id('id', 11, '主键', true)->create_uid()->create_key('id');
|
||||
}
|
||||
//执行操作数据库,建立数据表
|
||||
$result = $sql->end_table($data['title'], $data['engine_type'])->create();
|
||||
if ($result) {
|
||||
$id = $this->validate('model.add')->save($data);
|
||||
if (false === $id) {
|
||||
return array('info'=>$this->getError(), 'status'=>0);
|
||||
}else{
|
||||
return array('info' => $this->getError(), 'status' => 0);
|
||||
} else {
|
||||
// 清除模型缓存数据
|
||||
cache('document_model_list', null);
|
||||
|
||||
|
||||
//记录行为
|
||||
action_log('update_model', 'model', $id, session('auth_user.uid'));
|
||||
return $id ? array('info'=>'创建模型成功!','status'=>1) : array('info'=>'创建模型失败!','status'=>1);
|
||||
return $id ? array('info' => '创建模型成功!', 'status' => 1) : array('info' => '创建模型失败!', 'status' => 1);
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
//修改
|
||||
$status = $this->validate('model.edit')->save($data,array('id'=>$data['id']));
|
||||
$status = $this->validate('model.edit')->save($data, array('id' => $data['id']));
|
||||
if (false === $status) {
|
||||
return array('info'=>$this->getError(), 'status'=>0);
|
||||
}else{
|
||||
return array('info' => $this->getError(), 'status' => 0);
|
||||
} else {
|
||||
// 清除模型缓存数据
|
||||
cache('document_model_list', null);
|
||||
//记录行为
|
||||
action_log('update_model','model',$data['id'],session('auth_user.uid'));
|
||||
return array('info'=>'保存模型成功!','status'=>1);
|
||||
action_log('update_model', 'model', $data['id'], session('auth_user.uid'));
|
||||
return array('info' => '保存模型成功!', 'status' => 1);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
return array('info'=>$this->getError(),'status'=>0);
|
||||
} else {
|
||||
return array('info' => $this->getError(), 'status' => 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function del(){
|
||||
$id = input('id','','trim,intval');
|
||||
$model = $this->db()->where(array('id'=>$id))->find();
|
||||
public function del() {
|
||||
$id = input('id', '', 'trim,intval');
|
||||
$tablename = $this->where('id', $id)->value('name');
|
||||
|
||||
if ($model['extend'] == 0) {
|
||||
$this->error = "基础模型不允许删除!";
|
||||
return false;
|
||||
}elseif ($model['extend'] == 1){
|
||||
$tablename = 'document_'.$model['name'];
|
||||
}elseif ($model['extend'] == 2){
|
||||
$tablename = $model['name'];
|
||||
}
|
||||
//删除数据表
|
||||
$db = new \com\Datatable();
|
||||
if ($db->CheckTable($tablename)) {
|
||||
@@ -118,16 +106,16 @@ class Model extends Base{
|
||||
$this->error = "数据表删除失败!";
|
||||
}
|
||||
}
|
||||
$result = $this->db()->where(array('id'=>$id))->delete();
|
||||
$result = $this->where('id', $id)->delete();
|
||||
if ($result) {
|
||||
return true;
|
||||
}else{
|
||||
} else {
|
||||
$this->error = "模型删除失败!";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function attribute(){
|
||||
public function attribute() {
|
||||
return $this->hasMany('Attribute');
|
||||
}
|
||||
|
||||
@@ -136,10 +124,10 @@ class Model extends Base{
|
||||
* @param [array] $model [字段]
|
||||
* @return [array] [解析后的字段]
|
||||
*/
|
||||
public function preFields($model){
|
||||
$fields = $model->attribute;
|
||||
$groups = parse_config_attr($model['field_group']);
|
||||
$field_sort = json_decode($model['field_sort'],true);;
|
||||
public function preFields($model) {
|
||||
$fields = $model->attribute;
|
||||
$groups = parse_config_attr($model['field_group']);
|
||||
$field_sort = json_decode($model['field_sort'], true);
|
||||
|
||||
//获得数组的第一条数组
|
||||
$first_key = array_keys($groups);
|
||||
@@ -154,8 +142,8 @@ class Model extends Base{
|
||||
}
|
||||
}
|
||||
//未进行排序的放入第一组中
|
||||
$fields[] = array('name'=>'model_id','type'=>'hidden'); //加入模型ID值
|
||||
$fields[] = array('name'=>'id','type'=>'hidden'); //加入模型ID值
|
||||
$fields[] = array('name' => 'model_id', 'type' => 'hidden'); //加入模型ID值
|
||||
$fields[] = array('name' => 'id', 'type' => 'hidden'); //加入模型ID值
|
||||
foreach ($fields as $key => $value) {
|
||||
$groupfield[$first_key[0]][] = $value;
|
||||
}
|
||||
|
||||
@@ -29,5 +29,4 @@ class Model extends Base{
|
||||
'add' => 'name,title',
|
||||
'edit' => 'title',
|
||||
);
|
||||
|
||||
}
|
||||
@@ -13,45 +13,59 @@ use think\Db;
|
||||
* 数据库管理类
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
class Datatable{
|
||||
|
||||
protected $table; /*数据库操作的表*/
|
||||
protected $fields = array(); /*数据库操作字段*/
|
||||
protected $charset = 'utf8'; /*数据库操作字符集*/
|
||||
public $prefix = ''; /*数据库操作表前缀*/
|
||||
protected $model_table_prefix = ''; /*模型默认创建的表前缀*/
|
||||
protected $engine_type = 'MyISAM'; /*数据库引擎*/
|
||||
protected $key = 'id'; /*数据库主键*/
|
||||
public $sql = ''; /*最后生成的sql语句*/
|
||||
class Datatable {
|
||||
|
||||
protected $table; /*数据库操作的表*/
|
||||
protected $fields = array(); /*数据库操作字段*/
|
||||
protected $charset = 'utf8'; /*数据库操作字符集*/
|
||||
public $prefix = ''; /*数据库操作表前缀*/
|
||||
protected $model_table_prefix = ''; /*模型默认创建的表前缀*/
|
||||
protected $engine_type = 'MyISAM'; /*数据库引擎*/
|
||||
protected $key = 'id'; /*数据库主键*/
|
||||
public $sql = ''; /*最后生成的sql语句*/
|
||||
|
||||
/**
|
||||
* 初始化数据库信息
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function __construct(){
|
||||
public function __construct() {
|
||||
//创建DB对象
|
||||
$this->prefix = config('database.prefix');
|
||||
$this->prefix = config('database.prefix');
|
||||
$this->model_table_prefix = config('model_table_prefix');
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始创建表
|
||||
* @var $table 表名
|
||||
* @author colin <colin@tensent.cn>
|
||||
* @title 初始化表
|
||||
* @description 初始化创建表
|
||||
* @Author molong
|
||||
* @DateTime 2017-06-11
|
||||
* @param string $table 表名
|
||||
* @return void 空
|
||||
*/
|
||||
public function start_table($table){
|
||||
$this->table = $this->getTablename($table,true);
|
||||
$this->sql .= "CREATE TABLE IF NOT EXISTS `".$this->table."`(";
|
||||
return $this;
|
||||
}
|
||||
public function initTable($table = '', $comment = '', $pk = '', $time = true){
|
||||
$this->table = $this->getTablename($table, true);
|
||||
|
||||
/**
|
||||
* 创建字段
|
||||
* @var $sql 要执行的字段sql语句可以为array()或者strubf
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function create_field($sql){
|
||||
$this->sql .= $sql.',';
|
||||
if ($pk) {
|
||||
$sql[] = $this->generateField('id', 'int', 11, '', '主键', true);
|
||||
}
|
||||
if ($time) {
|
||||
//初始化表内含创建时间和更新时间两个字段
|
||||
$sql[] = $this->generateField('create_time', 'int', 11, 0, '创建时间', false);
|
||||
$sql[] = $this->generateField('update_time', 'int', 11, 0, '创建时间', false);
|
||||
}
|
||||
|
||||
$primary = $pk ? "PRIMARY KEY (`".$pk."`)" : '';
|
||||
if ($primary) {
|
||||
$generatesql = implode(',', $sql) . ',';
|
||||
}else{
|
||||
$generatesql = implode(',', $sql);
|
||||
}
|
||||
|
||||
$create = "CREATE TABLE IF NOT EXISTS `" . $this->table . "`("
|
||||
. $generatesql
|
||||
. $primary
|
||||
. ") ENGINE=" . $this->engine_type . " AUTO_INCREMENT=1 DEFAULT CHARSET=" . $this->charset . " ROW_FORMAT=DYNAMIC COMMENT='" . $comment . "';";
|
||||
$this->sql = $create;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -61,55 +75,51 @@ class Datatable{
|
||||
* @var comment 字段的描述
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function create_id($key = 'id', $length = 11 , $comment = '主键' , $is_auto_increment = true){
|
||||
$auto_increment = $is_auto_increment ? 'AUTO_INCREMENT' : '';
|
||||
$this->sql .= "`{$key}` int({$length}) unsigned NOT NULL $auto_increment COMMENT '{$comment}',";
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* 快速创建ID字段
|
||||
* @var length 字段的长度
|
||||
* @var comment 字段的描述
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function create_uid(){
|
||||
$this->sql .= "`uid` int(11) NOT NULL DEFAULT '0' COMMENT '用户uid',";
|
||||
return $this;
|
||||
public function generateField($key = '', $type = '', $length = 11, $default = '', $comment = '主键', $is_auto_increment = false){
|
||||
if ($key && $type) {
|
||||
$auto_increment = $is_auto_increment ? 'AUTO_INCREMENT' : '';
|
||||
$field_type = $length ? $type.'('.$length.')' : $type;
|
||||
$signed = in_array($type, array('int', 'float', 'double')) ? 'signed' : '';
|
||||
$comment = $comment ? "COMMENT '" . $comment . "'" : "";
|
||||
$default = $default ? "DEFAULT '" . $default . "'" : "";
|
||||
$sql = "`{$key}` {$field_type} {$signed} NOT NULL {$default} $auto_increment {$comment}";
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加字段
|
||||
* @var $table 追加字段的表名
|
||||
* @var $table 追加字段的表名
|
||||
* @var $attr 属性列表
|
||||
* @var $is_more 是否为多条同时插入
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function colum_field($table,$attr = array()){
|
||||
$field_attr['table'] = $table ? $this->getTablename($table,true) : $this->table;
|
||||
public function columField($table, $attr = array()) {
|
||||
$field_attr['table'] = $table ? $this->getTablename($table, true) : $this->table;
|
||||
$field_attr['field'] = $attr['field'];
|
||||
$field_attr['type'] = $attr['type'] ? $attr['type'] : 'varchar';
|
||||
$field_attr['type'] = $attr['type'] ? $attr['type'] : 'varchar';
|
||||
if (intval($attr['length']) && $attr['length']) {
|
||||
$field_attr['length'] = "(".$attr['length'].")";
|
||||
}else{
|
||||
$field_attr['length'] = "(" . $attr['length'] . ")";
|
||||
} else {
|
||||
$field_attr['length'] = "";
|
||||
}
|
||||
$field_attr['is_null'] = $attr['is_null'] ? 'NOT NULL' : 'null';
|
||||
$field_attr['default'] = $attr['default'] != '' ? 'default "'.$attr['default'].'"' : 'default null';
|
||||
if($field_attr['is_null'] == 'null'){
|
||||
$field_attr['default'] = $attr['default'] != '' ? 'default "' . $attr['default'] . '"' : 'default null';
|
||||
if ($field_attr['is_null'] == 'null') {
|
||||
$field_attr['default'] = $field_attr['default'];
|
||||
}else{
|
||||
} else {
|
||||
$field_attr['default'] = '';
|
||||
}
|
||||
$field_attr['comment'] = (isset($attr['comment']) && $attr['comment']) ? $attr['comment'] : '';
|
||||
$field_attr['oldname'] = (isset($attr['oldname']) && $attr['oldname']) ? $attr['oldname'] : '';
|
||||
$field_attr['newname'] = (isset($attr['newname']) && $attr['newname']) ? $attr['newname'] : $field_attr['field'];
|
||||
$field_attr['after'] = (isset($attr['after']) && $attr['after']) ? ' AFTER `'.$attr['after'].'`' : '';
|
||||
$field_attr['action'] = (isset($attr['action']) && $attr['action']) ? $attr['action'] : 'ADD';
|
||||
$field_attr['after'] = (isset($attr['after']) && $attr['after']) ? ' AFTER `' . $attr['after'] . '`' : '';
|
||||
$field_attr['action'] = (isset($attr['action']) && $attr['action']) ? $attr['action'] : 'ADD';
|
||||
//确认表是否存在
|
||||
|
||||
if($field_attr['action'] == 'ADD'){
|
||||
if ($field_attr['action'] == 'ADD') {
|
||||
$this->sql = "ALTER TABLE `{$field_attr['table']}` ADD `{$field_attr['field']}` {$field_attr['type']}{$field_attr['length']} {$field_attr['is_null']} {$field_attr['default']} COMMENT '{$field_attr['comment']}'";
|
||||
}elseif($field_attr['action'] == 'CHANGE'){
|
||||
} elseif ($field_attr['action'] == 'CHANGE') {
|
||||
$this->sql = "ALTER TABLE `{$field_attr['table']}` CHANGE `{$field_attr['oldname']}` `{$field_attr['newname']}` {$field_attr['type']}{$field_attr['length']} {$field_attr['is_null']} {$field_attr['default']} COMMENT '{$field_attr['comment']}'";
|
||||
}
|
||||
return $this;
|
||||
@@ -117,41 +127,27 @@ class Datatable{
|
||||
|
||||
/**
|
||||
* 删除字段
|
||||
* @var $table 追加字段的表名
|
||||
* @var $table 追加字段的表名
|
||||
* @var $field 字段名
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function del_field($table,$field){
|
||||
$table = $table ? $this->getTablename($table,true) : $this->table;
|
||||
public function delField($table, $field) {
|
||||
$table = $table ? $this->getTablename($table, true) : $this->table;
|
||||
$this->sql = "ALTER TABLE `$table` DROP `$field`";
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据表
|
||||
* @var $table 追加字段的表名
|
||||
* @var $table 追加字段的表名
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function del_table($table){
|
||||
$table = $table ? $this->getTablename($table,true) : $this->table;
|
||||
public function delTable($table) {
|
||||
$table = $table ? $this->getTablename($table, true) : $this->table;
|
||||
$this->sql = "DROP TABLE `$table`";
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 主键设置
|
||||
* @var $key 要被设置主键的字段
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function create_key($key = null){
|
||||
if(null != $key){
|
||||
$this->key = $key;
|
||||
}
|
||||
$this->sql .= "PRIMARY KEY (`".$this->key."`)";
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 结束表
|
||||
* @var $engine_type 数据库引擎
|
||||
@@ -159,15 +155,15 @@ class Datatable{
|
||||
* @var $charset 数据库编码
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function end_table($comment,$engine_type = null,$charset = null){
|
||||
if(null != $charset){
|
||||
public function endTable($comment, $engine_type = null, $charset = null) {
|
||||
if (null != $charset) {
|
||||
$this->charset = $charset;
|
||||
}
|
||||
if(null != $engine_type){
|
||||
if (null != $engine_type) {
|
||||
$this->engine_type = $engine_type;
|
||||
}
|
||||
$end = "ENGINE=".$this->engine_type." AUTO_INCREMENT=1 DEFAULT CHARSET=".$this->charset." ROW_FORMAT=DYNAMIC COMMENT='".$comment."';";
|
||||
$this->sql .= ")".$end;
|
||||
$end = "ENGINE=" . $this->engine_type . " AUTO_INCREMENT=1 DEFAULT CHARSET=" . $this->charset . " ROW_FORMAT=DYNAMIC COMMENT='" . $comment . "';";
|
||||
$this->sql .= ")" . $end;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -176,7 +172,7 @@ class Datatable{
|
||||
* @return int 0
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function create(){
|
||||
public function create() {
|
||||
$res = Db::execute($this->sql);
|
||||
return $res !== false;
|
||||
}
|
||||
@@ -186,7 +182,7 @@ class Datatable{
|
||||
* @return int 0
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function query(){
|
||||
public function query() {
|
||||
return $this->create();
|
||||
}
|
||||
|
||||
@@ -194,7 +190,7 @@ class Datatable{
|
||||
* 获取最后生成的sql语句
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function getLastSql(){
|
||||
public function getLastSql() {
|
||||
return $this->sql;
|
||||
}
|
||||
|
||||
@@ -204,11 +200,11 @@ class Datatable{
|
||||
* @var $prefix 获取表前缀? 默认为不获取 false
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function getTablename($table , $prefix = false){
|
||||
if(false == $prefix){
|
||||
$this->table = $this->model_table_prefix.$table;
|
||||
}else{
|
||||
$this->table = $this->prefix.$this->model_table_prefix.$table;
|
||||
public function getTablename($table, $prefix = false) {
|
||||
if (false == $prefix) {
|
||||
$this->table = $this->model_table_prefix . $table;
|
||||
} else {
|
||||
$this->table = $this->prefix . $this->model_table_prefix . $table;
|
||||
}
|
||||
return $this->table;
|
||||
}
|
||||
@@ -218,23 +214,23 @@ class Datatable{
|
||||
* @var $table 要获取名字的表名 可以为sent_tengsu_photo、tengsu_photo、photo
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function getFields($table){
|
||||
if(false == $table){
|
||||
$table = $this->table;//为空调用当前table
|
||||
}else{
|
||||
public function getFields($table) {
|
||||
if (false == $table) {
|
||||
$table = $this->table; //为空调用当前table
|
||||
} else {
|
||||
$table = $table;
|
||||
}
|
||||
$patten = "/\./";
|
||||
if(!preg_match_all($patten,$table)){
|
||||
if (!preg_match_all($patten, $table)) {
|
||||
//匹配_
|
||||
$patten = "/_+/";
|
||||
if(!preg_match_all($patten, $table)){
|
||||
$table = $this->prefix.$this->model_table_prefix.$table;
|
||||
}else{
|
||||
if (!preg_match_all($patten, $table)) {
|
||||
$table = $this->prefix . $this->model_table_prefix . $table;
|
||||
} else {
|
||||
//匹配是否包含表前缀,如果是 那么就是手动输入
|
||||
$patten = "/$this->prefix/";
|
||||
if(!preg_match_all($patten,$table)){
|
||||
$table = $this->prefix.$table;
|
||||
if (!preg_match_all($patten, $table)) {
|
||||
$table = $this->prefix . $table;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -248,10 +244,10 @@ class Datatable{
|
||||
* @author colin <colin@tensent.cn>
|
||||
* @return boolen
|
||||
*/
|
||||
public function CheckTable($table){
|
||||
public function CheckTable($table) {
|
||||
//获取表名
|
||||
$this->table = $this->getTablename($table,true);
|
||||
$result = Db::execute("SHOW TABLES LIKE '%$this->table%'");
|
||||
$this->table = $this->getTablename($table, true);
|
||||
$result = Db::execute("SHOW TABLES LIKE '%$this->table%'");
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -262,12 +258,12 @@ class Datatable{
|
||||
* @author colin <colin@tensent.cn>
|
||||
* @return boolen
|
||||
*/
|
||||
public function CheckField($table,$field){
|
||||
public function CheckField($table, $field) {
|
||||
//检查字段是否存在
|
||||
$table = $this->getTablename($table,true);
|
||||
if(!Db::query("Describe $table $field")){
|
||||
$table = $this->getTablename($table, true);
|
||||
if (!Db::query("Describe $table $field")) {
|
||||
return false;
|
||||
}else{
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user