diff --git a/application/admin/controller/Attribute.php b/application/admin/controller/Attribute.php index 34492284..1305ecd7 100644 --- a/application/admin/controller/Attribute.php +++ b/application/admin/controller/Attribute.php @@ -46,19 +46,7 @@ class Attribute extends Admin { * @author colin */ 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 */ - 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); + } } \ No newline at end of file diff --git a/application/admin/controller/Content.php b/application/admin/controller/Content.php index faf37f39..07481bce 100644 --- a/application/admin/controller/Content.php +++ b/application/admin/controller/Content.php @@ -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; } diff --git a/application/admin/controller/Model.php b/application/admin/controller/Model.php index a5a5f13a..a4a9c361 100644 --- a/application/admin/controller/Model.php +++ b/application/admin/controller/Model.php @@ -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 */ - 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 + * @author molong */ - 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 */ 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 */ - 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('状态设置成功!'); } } \ No newline at end of file diff --git a/application/admin/static/css/style.css b/application/admin/static/css/style.css index 1cf453cf..661d4a20 100644 --- a/application/admin/static/css/style.css +++ b/application/admin/static/css/style.css @@ -7270,4 +7270,6 @@ to { padding-left:48px; } } -span.reloadverify{padding: 0; overflow: hidden; cursor: pointer;} \ No newline at end of file +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;} \ No newline at end of file diff --git a/application/admin/view/attribute/index.html b/application/admin/view/attribute/index.html index bf35269d..0ef64819 100644 --- a/application/admin/view/attribute/index.html +++ b/application/admin/view/attribute/index.html @@ -6,9 +6,13 @@

{$meta_title}

- 返回 - 新 增 - + 新 增 + {if $model_id} + + + {/if} + + 返回
@@ -36,7 +40,7 @@ {$item['value']} 编辑 - 删除 + 删除 {/volist} @@ -46,4 +50,19 @@
+{/block} +{block name="script"} + + {/block} \ No newline at end of file diff --git a/application/admin/view/attribute/insert.html b/application/admin/view/attribute/insert.html new file mode 100644 index 00000000..ae2bdeda --- /dev/null +++ b/application/admin/view/attribute/insert.html @@ -0,0 +1,60 @@ +{include file="admin@public/header" /} +
+
+
+
+

{$meta_title}

+
+
+
+
+ + + + + + + + + + + + + {volist name="list" id="item"} + + + + + + + + + {/volist} + +
表单标题字段名字段类型字段长度默认值
{$item['title']}{$item['name']}{$item['type_text']}{$item['length']}{$item['value']}
+ {$page} +
+
+
+
+ + +{include file="admin@public/footer" /} \ No newline at end of file diff --git a/application/admin/view/model/add.html b/application/admin/view/model/add.html index 9dfe7a46..c3f825aa 100644 --- a/application/admin/view/model/add.html +++ b/application/admin/view/model/add.html @@ -12,7 +12,7 @@
-
+
@@ -29,50 +29,6 @@ (请输入模型的名称)
-
- -
- - (目前只支持独立模型和文档模型) -
-
-
- -
- - -
-
-
- -
- - (选“是”则会新建默认的id字段作为主键) -
-
-
- -
- - (选“是”则会新建默认的id字段作为主键) -
-
@@ -85,7 +41,6 @@
-
diff --git a/application/admin/view/model/edit.html b/application/admin/view/model/edit.html index ae134c6b..be6c8f1e 100644 --- a/application/admin/view/model/edit.html +++ b/application/admin/view/model/edit.html @@ -6,13 +6,13 @@
-

{if condition="ACTION_NAME eq 'add'"}新增{else/}编辑{/if}模型

+

编辑模型

- +
-
- -
- - (目前只支持独立模型和文档模型) -
-
-
- -
- - (选“是”则会新建默认的id字段作为主键) -
-
@@ -70,21 +49,21 @@
- + (用于表单显示的名称)
- + (用于表单显示的分组,以及设置该模型表单排序的显示)
-
- {volist name=":parse_field_attr($info['field_group'])" id="vo"} +
+ {volist name=":parse_field_attr($info['attribute_group'])" id="vo"}
{$vo}
@@ -93,7 +72,7 @@ {if (($field['group'] == -1) or ($field['group'] eq $key))}
{$field['title']} [{$field['name']}] - +
{php} unset($fields[$k]); @@ -112,7 +91,7 @@
- + (默认列表模板的展示规则)
@@ -120,14 +99,14 @@
- + (默认列表模板的默认搜索项)
- + (默认列表模板的高级搜索项)
@@ -180,11 +159,11 @@ - - +{include file="admin@public/header" /} {block name="style"}{/block} - - -
{include file="admin@public/tool" /} - - - - - - - - - - - -{block name="script"}{/block} - - \ No newline at end of file +{include file="admin@public/footer" /} +{block name="script"}{/block} \ No newline at end of file diff --git a/application/admin/view/public/footer.html b/application/admin/view/public/footer.html new file mode 100644 index 00000000..307bc686 --- /dev/null +++ b/application/admin/view/public/footer.html @@ -0,0 +1,24 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/application/admin/view/public/header.html b/application/admin/view/public/header.html new file mode 100644 index 00000000..54479c14 --- /dev/null +++ b/application/admin/view/public/header.html @@ -0,0 +1,23 @@ + + + + + + +SentCMS网站管理系统后台 + + + + + + + + + + + \ No newline at end of file diff --git a/application/common.php b/application/common.php index 87e0e4d4..c2bcf1b6 100644 --- a/application/common.php +++ b/application/common.php @@ -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); //字符串解密加密 diff --git a/application/common/behavior/InitHook.php b/application/common/behavior/InitHook.php index 22ba4792..40153a98 100644 --- a/application/common/behavior/InitHook.php +++ b/application/common/behavior/InitHook.php @@ -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']; diff --git a/application/common/controller/Admin.php b/application/common/controller/Admin.php index d31447d2..dedb42ec 100644 --- a/application/common/controller/Admin.php +++ b/application/common/controller/Admin.php @@ -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(); diff --git a/application/common/model/Attribute.php b/application/common/model/Attribute.php index 91bd2a6a..ad744efa 100644 --- a/application/common/model/Attribute.php +++ b/application/common/model/Attribute.php @@ -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(); + }; + } } \ No newline at end of file diff --git a/application/common/model/Model.php b/application/common/model/Model.php index 527d5a86..4bc39f25 100644 --- a/application/common/model/Model.php +++ b/application/common/model/Model.php @@ -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; } diff --git a/application/common/validate/Model.php b/application/common/validate/Model.php index ec9bb246..9f91cb73 100644 --- a/application/common/validate/Model.php +++ b/application/common/validate/Model.php @@ -29,5 +29,4 @@ class Model extends Base{ 'add' => 'name,title', 'edit' => 'title', ); - } \ No newline at end of file diff --git a/core/extend/com/Datatable.php b/core/extend/com/Datatable.php index 0706123a..f88415eb 100644 --- a/core/extend/com/Datatable.php +++ b/core/extend/com/Datatable.php @@ -13,45 +13,59 @@ use think\Db; * 数据库管理类 * @author colin */ -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 */ - 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 + * @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 - */ - 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 */ - 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 - */ - 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 */ - 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 */ - 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 */ - 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 - */ - 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 */ - 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 */ - 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 */ - public function query(){ + public function query() { return $this->create(); } @@ -194,7 +190,7 @@ class Datatable{ * 获取最后生成的sql语句 * @author colin */ - public function getLastSql(){ + public function getLastSql() { return $this->sql; } @@ -204,11 +200,11 @@ class Datatable{ * @var $prefix 获取表前缀? 默认为不获取 false * @author colin */ - 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 */ - 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 * @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 * @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; } }