模型基本雏形

This commit is contained in:
2017-06-19 00:09:18 +08:00
parent ac1e6a634f
commit 571b9841b1
13 changed files with 372 additions and 373 deletions

View File

@@ -46,7 +46,17 @@ class Attribute extends Admin {
* @author colin <colin@tensent.cn>
*/
public function index($model_id = null) {
$this->getAttributeList($model_id);
if (!$model_id) {
return $this->error('非法操作!');
}
$list = model('Attribute')->where('model_id', $model_id)->order('id desc')->paginate(25);
$data = array(
'list' => $list,
'model_id' => $model_id,
'page' => $list->render(),
);
$this->assign($data);
$this->setMeta('字段管理');
return $this->fetch();
}
@@ -55,19 +65,15 @@ class Attribute extends Admin {
* 创建字段
* @author colin <colin@tensent.cn>
*/
public function add() {
$model_id = input('model_id', '', 'trim,intval');
public function add($model_id = '') {
if (IS_POST) {
$result = $this->model->change();
if ($result) {
$result = $this->model->validate('attribute.add')->save($this->param);
if (false !== $result) {
return $this->success("创建成功!", url('Attribute/index', array('model_id' => $model_id)));
} else {
return $this->error($this->model->getError());
}
} else {
if (!$model_id) {
return $this->error('非法操作!');
}
$data = array(
'info' => array('model_id' => $model_id),
'fieldGroup' => $this->field,
@@ -82,16 +88,15 @@ class Attribute extends Admin {
* 编辑字段方法
* @author colin <colin@tensent.cn>
*/
public function edit() {
public function edit($id = '', $model_id = '') {
if (IS_POST) {
$result = $this->model->change();
$result = $this->model->validate('attribute.edit')->save($this->param, array('id'=>$id));
if ($result) {
return $this->success("修改成功!", url('Attribute/index', array('model_id' => $_POST['model_id'])));
return $this->success("修改成功!", url('Attribute/index', array('model_id' => $model_id)));
} else {
return $this->error($this->model->getError());
}
} else {
$id = input('id', '', 'trim,intval');
$info = db('Attribute')->find($id);
$data = array(
'info' => $info,
@@ -129,6 +134,7 @@ class Attribute extends Admin {
$model = model('Model')->where('id', $id)->find();
$result = $this->model->generate($model);
if (false !== $result) {
db('Model')->where('id', $id)->setField('table_status', 1);
return $this->success('生成成功!', url('admin/model/index'));
} else {
return $this->error($this->model->getError());
@@ -138,33 +144,6 @@ class Attribute extends Admin {
}
}
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(
@@ -192,21 +171,4 @@ 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);
}
}

View File

@@ -22,7 +22,7 @@ class Content extends Admin {
return $this->error("无此模型!");
} else {
$this->modelInfo = $list[$model_id];
$this->model = model('Content')->extend($this->modelInfo['name']);
$this->model = new \app\common\model\Content($this->modelInfo['name']);
}
$this->assign('model_id', $model_id);
@@ -44,7 +44,7 @@ class Content extends Admin {
$field = array_filter($grid_list['fields']);
$list = $this->model->lists($map, $order);
$list = $this->model->where($map)->order($order)->paginate($this->modelInfo['list_row']);
$data = array(
'grid' => $grid_list,
@@ -67,7 +67,7 @@ class Content extends Admin {
*/
public function add() {
if (IS_POST) {
$result = $this->model->change();
$result = $this->model->save($this->param);
if ($result) {
return $this->success("添加成功!", url('admin/content/index', array('model_id' => $this->modelInfo['id'])));
} else {
@@ -98,7 +98,7 @@ class Content extends Admin {
*/
public function edit($id) {
if (IS_POST) {
$result = $this->model->change();
$result = $this->model->save($this->param, array('id'=> $id));
if ($result !== false) {
return $this->success("更新成功!", url('admin/content/index', array('model_id' => $this->modelInfo['id'])));
} else {
@@ -108,7 +108,7 @@ class Content extends Admin {
if (!$id) {
return $this->error("非法操作!");
}
$info = $this->model->detail($id);
$info = $this->model->find($id);
if (!$info) {
return $this->error($this->model->getError());
}
@@ -186,16 +186,9 @@ class Content extends Admin {
* @author molong <ycgpp@126.com>
*/
protected function getField() {
$field_group = parse_config_attr($this->modelInfo['field_group']);
$field_sort = json_decode($this->modelInfo['field_sort'], true);
$field_group = parse_config_attr($this->modelInfo['attribute_group']);
if ($this->modelInfo['extend'] > 1) {
$map['model_id'] = $this->modelInfo['id'];
} else {
$model_id[] = $this->modelInfo['id'];
$model_id[] = 1;
$map['model_id'] = array('IN', $model_id);
}
$map['model_id'] = $this->modelInfo['id'];
if ($this->request->action() == 'add') {
$map['is_show'] = array('in', array('1', '2'));
} elseif ($this->request->action() == 'edit') {
@@ -204,30 +197,13 @@ class Content extends Admin {
//获得数组的第一条数组
$first_key = array_keys($field_group);
$fields = model('Attribute')->getFieldlist($map);
if (!empty($field_sort)) {
foreach ($field_sort as $key => $value) {
foreach ($value as $index) {
if (isset($fields[$index])) {
$groupfield[$key][] = $fields[$index];
unset($fields[$index]);
}
}
$rows = model('Attribute')->where($map)->order('group_id asc, sort asc')->select();
if (!empty($rows)) {
foreach ($rows as $key => $value) {
$fields[$field_group[$value['group_id']]][] = $value;
}
}
//未进行排序的放入第一组中
$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;
}
foreach ($field_group as $key => $value) {
if ($groupfield[$key]) {
$data[$value] = $groupfield[$key];
}
}
return $data;
return $fields;
}
/**
@@ -253,6 +229,9 @@ class Content extends Admin {
if (isset($map['page'])) {
unset($map['page']);
}
if (isset($map['model_id'])) {
unset($map['model_id']);
}
$this->assign($data);
return $map;
}

View File

@@ -49,6 +49,8 @@ class Model extends Admin {
if (IS_POST) {
$result = $this->model->validate('Model.add')->save($request->post());
if (false !== $result) {
//记录行为
action_log('add_model', 'model', $result, session('auth_user.uid'));
$this->success('创建成功!', url('admin/model/index'));
}else{
return $this->error($this->model->getError());
@@ -67,6 +69,8 @@ class Model extends Admin {
if (IS_POST) {
$result = $this->model->validate('Model.edit')->save($request->post(), array('id'=>$request->post('id')));
if (false !== $result) {
//记录行为
action_log('update_model', 'model', $request->post('id'), session('auth_user.uid'));
$this->success('更新成功!', url('admin/model/index'));
}else{
return $this->error($this->model->getError());
@@ -75,31 +79,11 @@ class Model extends Admin {
$info = $this->model->where('id', $request->param('id'))->find();
//获取字段列表
if ($info['attribute_list']) {
$fields = model('Attribute')->where('id', 'IN', $info['attribute_list'])->where('is_show', 1)->select();
$rows = db('Attribute')->where('model_id', $request->param('id'))->where('is_show', 1)->order('group_id asc, sort asc')->select();
if ($rows) {
// 梳理属性的可见性
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");
foreach ($rows as $key => $field) {
$fields[$field['group_id']][] = $field;
}
}else{
$fields = array();

View File

@@ -7,10 +7,6 @@
</div>
<div class="pull-right">
<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>
@@ -39,7 +35,7 @@
<td>{$item['length']}</td>
<td>{$item['value']}</td>
<td>
<a href="{:url('edit',array('id'=>$item['id']))}">编辑</a>
<a href="{:url('edit',array('id'=>$item['id'], 'model_id'=>$model_id))}">编辑</a>
<a href="{:url('del',array('id'=>$item['id'], 'model_id'=>$model_id))}" class="confirm ajax-get">删除</a>
</td>
</tr>

View File

@@ -1,60 +0,0 @@
{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" /}

View File

@@ -63,21 +63,16 @@
<div class="form-group">
<label class="col-lg-2 control-label">表单显示排序</label>
<div class="col-lg-10 boards" id="attribute_group_sort">
{volist name=":parse_field_attr($info['attribute_group'])" id="vo"}
{volist name="fields" id="field"}
<div class="board panel panel-info">
<div class="panel-heading">{$vo}</div>
<div class="panel-heading">{:parse_field_attr($info['attribute_group'])[$key]}</div>
<div class="panel-body">
<div class="board-list" data-group="{$key}">
{foreach name="fields" item="field" key="k"}
{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="attribute_sort[{$key}][]" value="{$field['id']}"/>
</div>
{php}
unset($fields[$k]);
{/php}
{/if}
{foreach name="field" item="item" key="k"}
<div class="board-item">
<span data="{$item['id']}">{$item['title']} [{$item['name']}]</span>
<input type="hidden" name="attribute_sort[{$key}][]" value="{$item['id']}"/>
</div>
{/foreach}
</div>
</div>

View File

@@ -18,6 +18,39 @@ class Attribute extends Base{
'id' => 'integer',
);
protected static function init(){
self::afterInsert(function($data){
if ($data['model_id']) {
$name = db('Model')->where('id', $data['model_id'])->value('name');
$db = new \com\Datatable();
$attr = $data->toArray();
$model_attr = array(
'model_id' => $data['model_id'],
'attr_id' => $data->id,
'group_id' => 0,
'is_add_table' => 1,
'is_show' => $data['is_show'],
'is_must' => $data['is_must'],
'sort' => 0,
);
$attr['after'] = db('Attribute')->where('model_id', $data['model_id'])->order('id desc')->value('name');
return $db->columField(strtolower($name), $attr)->query();
}
});
self::beforeUpdate(function($data){
$attr = $data->toArray();
$attr['action'] = 'CHANGE';
$attr['oldname'] = db('Attribute')->where('id', $attr['id'])->value('name');
if ($attr['id']) {
$name = db('Model')->where('id', $attr['model_id'])->value('name');
$db = new \com\Datatable();
return $db->columField(strtolower($name), $attr)->query();
}else{
return false;
}
});
}
protected function getTypeTextAttr($value, $data){
$type = config('config_type_list');
$type_text = explode(',', $type[$data['type']]);
@@ -140,7 +173,7 @@ class Attribute extends Base{
//检查表是否存在并创建
if (!$db->CheckTable($tablename)) {
//创建新表
$db->initTable($tablename, $model['title'], 'id')->query();
return $db->initTable($tablename, $model['title'], 'id')->query();
};
}
}

View File

@@ -9,89 +9,230 @@
namespace app\common\model;
use \think\Validate;
/**
* 设置模型
*/
class Content extends Base{
* 设置模型
*/
class Content {
protected $dao;
protected $data;
protected $autoWriteTimestamp = true;
protected $auto = array('update_time');
protected $insert = array('create_time');
protected $update = array();
// 创建时间字段
protected $createTime = 'create_time';
// 更新时间字段
protected $updateTime = 'update_time';
// 时间字段取出后的默认时间格式
protected $dateFormat;
// 字段类型或者格式转换
protected $type = [];
protected $auto = array("update_time");
protected $insert = array("create_time");
protected $type = array(
'id' => 'integer',
'cover_id' => 'integer',
'create_time' => 'integer',
'update_time' => 'integer',
);
protected function setUidAttr(){
return session('user_auth.uid');
public function __construct($name) {
$this->db = db($name);
}
protected function setCreateTimeAttr($value){
return $value ? strtotime($value) : time();
public function save($data, $where = array()) {
$this->data = $data;
$rule = $msg = array();
$attr = db('Attribute')->where('id', $data['model_id'])->select();
foreach ($attr as $key => $value) {
if ($value['is_must'] == 1) {
$rule[$value['name']] = "require";
$msg[$value['name'] . '.require'] = $value['title'] . "不能为空!";
}
}
$validate = new Validate($rule, $msg);
$result = $validate->check($data);
if (!$result) {
$this->error = $validate->getError();
return false;
}
$this->autoCompleteData($this->auto);
if (!empty($where)) {
$this->autoCompleteData($this->update);
return $this->where($where)->update($this->data);
} else {
$this->autoCompleteData($this->insert);
return $this->insert($this->data);
}
}
protected function setUpdateTimeAttr($value){
return $value ? strtotime($value) : time();
/**
* 返回模型的错误信息
* @access public
* @return string|array
*/
public function getError() {
return $this->error;
}
public function extend($name){
$this->dao = db($name);
/**
* 数据自动完成
* @access public
* @param array $auto 要自动更新的字段列表
* @return void
*/
protected function autoCompleteData($auto = []) {
foreach ($auto as $field => $value) {
if (is_integer($field)) {
$field = $value;
$value = null;
}
if (!isset($this->data[$field])) {
$default = null;
} else {
$default = $this->data[$field];
}
$this->setAttr($field, !is_null($value) ? $value : $default);
}
}
/**
* 修改器 设置数据对象值
* @access public
* @param string $name 属性名
* @param mixed $value 属性值
* @param array $data 数据
* @return $this
*/
public function setAttr($name, $value, $data = []) {
if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) {
// 自动写入的时间戳字段
$value = $this->autoWriteTimestamp($name);
} else {
// 检测修改器
$method = 'set' . Loader::parseName($name, 1) . 'Attr';
if (method_exists($this, $method)) {
$value = $this->$method($value, array_merge($this->data, $data));
} elseif (isset($this->type[$name])) {
// 类型转换
$value = $this->writeTransform($value, $this->type[$name]);
}
}
// 设置数据对象属性
$this->data[$name] = $value;
return $this;
}
public function lists($map, $order){
$list = $this->dao->where($map)->order($order)->paginate(15, false, array(
'query' => $this->param,
));
return $list;
/**
* 自动写入时间戳
* @access public
* @param string $name 时间戳字段
* @return mixed
*/
protected function autoWriteTimestamp($name) {
if (isset($this->type[$name])) {
$type = $this->type[$name];
if (strpos($type, ':')) {
list($type, $param) = explode(':', $type, 2);
}
switch ($type) {
case 'datetime':
case 'date':
$format = !empty($param) ? $param : $this->dateFormat;
$value = $this->formatDateTime(time(), $format);
break;
case 'timestamp':
case 'integer':
default:
$value = time();
break;
}
} elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [
'datetime',
'date',
'timestamp',
])
) {
$value = $this->formatDateTime(time(), $this->dateFormat);
} else {
$value = $this->formatDateTime(time(), $this->dateFormat, true);
}
return $value;
}
public function detail($id, $map = array()){
$map['id'] = $id;
$this->data = $this->dao->where($map)->find();
return $this->data;
/**
* 时间日期字段格式化处理
* @access public
* @param mixed $time 时间日期表达式
* @param mixed $format 日期格式
* @param bool $timestamp 是否进行时间戳转换
* @return mixed
*/
protected function formatDateTime($time, $format, $timestamp = false) {
if (false !== strpos($format, '\\')) {
$time = new $format($time);
} elseif (!$timestamp && false !== $format) {
$time = date($format, $time);
}
return $time;
}
public function del($map){
return $this->dao->where($map)->delete();
/**
* 数据写入 类型转换
* @access public
* @param mixed $value 值
* @param string|array $type 要转换的类型
* @return mixed
*/
protected function writeTransform($value, $type) {
if (is_null($value)) {
return;
}
if (is_array($type)) {
list($type, $param) = $type;
} elseif (strpos($type, ':')) {
list($type, $param) = explode(':', $type, 2);
}
switch ($type) {
case 'integer':
$value = (int) $value;
break;
case 'float':
if (empty($param)) {
$value = (float) $value;
} else {
$value = (float) number_format($value, $param, '.', '');
}
break;
case 'boolean':
$value = (bool) $value;
break;
case 'timestamp':
if (!is_numeric($value)) {
$value = strtotime($value);
}
break;
case 'datetime':
$format = !empty($param) ? $param : $this->dateFormat;
$value = is_numeric($value) ? $value : strtotime($value);
$value = $this->formatDateTime($value, $format);
break;
case 'object':
if (is_object($value)) {
$value = json_encode($value, JSON_FORCE_OBJECT);
}
break;
case 'array':
$value = (array) $value;
case 'json':
$option = !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE;
$value = json_encode($value, $option);
break;
case 'serialize':
$value = serialize($value);
break;
}
return $value;
}
public function change(){
$data = $this->param;
if (isset($data['id']) && $data['id']) {
$where['id'] = $data['id'];
}
if (!empty($data)) {
// 数据自动验证
if (!$this->validateData($data)) {
return false;
}
// 数据对象赋值
foreach ($data as $key => $value) {
$this->setAttr($key, $value, $data);
}
if (!empty($where)) {
$this->isUpdate = true;
}
}
// 数据自动完成
$this->autoCompleteData($this->auto);
// 自动写入更新时间
if ($this->autoWriteTimestamp && $this->updateTime) {
$this->setAttr($this->updateTime, null);
}
if ($this->isUpdate) {
$result = $this->dao->update($this->data, $where);
}else{
$result = $this->dao->insert($this->data);
}
return $result;
public function __call($method, $args) {
return call_user_func_array([$this->db, $method], $args);
}
}

View File

@@ -22,6 +22,22 @@ class Model extends Base {
'update_time' => 'integer',
);
protected static function init(){
self::beforeUpdate(function($event){
$data = $event->toArray();
$attribute_sort = json_decode($data['attribute_sort'], true);
if (!empty($attribute_sort)) {
foreach ($attribute_sort as $key => $value) {
db('Attribute')->where('id', 'IN', $value)->setField('group_id', $key);
foreach ($value as $k => $v) {
db('Attribute')->where('id', $v)->setField('sort', $k);
}
}
}
return true;
});
}
protected function setAttributeSortAttr($value){
return $value ? json_encode($value) : '';
}
@@ -38,60 +54,6 @@ class Model extends Base {
return $status[$data['status']];
}
/**
* 更新一个或新增一个模型
* @return array
*/
public function change() {
if (IS_POST) {
$data = \think\Request::instance()->post();
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');
}
//执行操作数据库,建立数据表
$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 {
// 清除模型缓存数据
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);
}
} else {
return false;
}
} else {
//修改
$status = $this->validate('model.edit')->save($data, array('id' => $data['id']));
if (false === $status) {
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);
}
}
} else {
return array('info' => $this->getError(), 'status' => 0);
}
}
}
public function del() {
$id = input('id', '', 'trim,intval');
$tablename = $this->where('id', $id)->value('name');

View File

@@ -10,29 +10,32 @@
namespace app\common\validate;
/**
* 设置模型
*/
class Attribute extends Base{
* 设置模型
*/
class Attribute extends Base {
protected $rule = array(
'name' => 'require|/^[a-zA-Z]\w{0,39}$/',
'title' => 'require',
'name' => 'require|unique:attribute|/^[a-zA-Z]\w{0,39}$/',
'title' => 'require',
'type' => 'require',
'length' => 'requireIn:type,textarea,editor|integer',
'remark' => 'require',
'length' => 'requireIn:type,textarea,editor|integer',
'remark' => 'require',
'value' => 'requireIf:is_must,1'
);
protected $message = array(
'length.requireIn' => '字段长度必须!',
'length.integer' => '字段必须为整形',
'name.require' => '字段名不能为空!',
'length.requireIn' => '字段长度必须!',
'length.integer' => '字段必须为整形',
'name.require' => '字段名不能为空!',
'name.unique' => '字段名已存在!',
'title.require' => '字段标题不能为空!',
'type.require' => '类型不能为空!',
'remark.require' => '描述不能为空!',
'type.require' => '类型不能为空!',
'remark.require' => '描述不能为空!',
'value' => '必填字段默认值必须!'
);
protected $scene = array(
'add' => 'name,title,type,remark,length',
'edit' => 'name,title,type,remark,length'
'add' => 'name,title,type,remark,length,value',
'edit' => 'name,title,type,remark,length,value',
);
}

View File

@@ -123,12 +123,7 @@ class Content extends Fornt {
//模块内容详情页
public function detail($id = '', $name = '') {
//当为文章模型时
$info = $this->model->detail($id);
if ($this->modelInfo['extend'] = 1 && (time() - session('set_content_view')) > 1800) {
db('Document')->where(array('id' => $id))->setInc('view');
session('set_content_view', time());
}
$info = $this->model->find($id);
if (empty($info)) {
return $this->error("无此内容!");
@@ -168,11 +163,7 @@ class Content extends Fornt {
return $this->error("无此模型!");
} else {
$this->modelInfo = $model_name ? $name_list[$model_name] : $id_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 = new \app\common\model\Content($this->modelInfo['name']);
$this->assign('model_id', $this->modelInfo['id']);
$this->assign('model_list', $name_list);

View File

@@ -13,8 +13,7 @@ use app\common\controller\Fornt;
class Index extends Fornt {
//网站首页
public function index() {
//设置SEO
public function index() { //设置SEO
$this->setSeo(config('web_site_title'), config('web_site_keyword'), config('web_site_description'));
return $this->fetch();
}

View File

@@ -23,6 +23,24 @@ class Datatable {
protected $engine_type = 'MyISAM'; /*数据库引擎*/
protected $key = 'id'; /*数据库主键*/
public $sql = ''; /*最后生成的sql语句*/
protected $typeAlist = array(
"text" => "VARCHAR",
"string" => "VARCHAR",
"password" => "VARCHAR",
"textarea" => "TEXT",
"bool" => "INT",
"select" => "INT",
"num" => "INT",
"decimal" => "DECIMAL",
"tags" => "VARCHAR",
"datetime" => "INT",
"date" => "INT",
"editor" => "TEXT",
"bind" => "INT",
"image" => "INT",
"images" => "VARCHAR",
"attach" => "VARCHAR",
);
/**
* 初始化数据库信息
@@ -42,7 +60,7 @@ class Datatable {
* @param string $table 表名
* @return void 空
*/
public function initTable($table = '', $comment = '', $pk = '', $time = true){
public function initTable($table = '', $comment = '', $pk = '', $time = true) {
$this->table = $this->getTablename($table, true);
if ($pk) {
@@ -54,17 +72,17 @@ class Datatable {
$sql[] = $this->generateField('update_time', 'int', 11, 0, '创建时间', false);
}
$primary = $pk ? "PRIMARY KEY (`".$pk."`)" : '';
$primary = $pk ? "PRIMARY KEY (`" . $pk . "`)" : '';
if ($primary) {
$generatesql = implode(',', $sql) . ',';
}else{
} 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 . "';";
. $generatesql
. $primary
. ") ENGINE=" . $this->engine_type . " AUTO_INCREMENT=1 DEFAULT CHARSET=" . $this->charset . " ROW_FORMAT=DYNAMIC COMMENT='" . $comment . "';";
$this->sql = $create;
return $this;
}
@@ -75,14 +93,14 @@ class Datatable {
* @var comment 字段的描述
* @author colin <colin@tensent.cn>
*/
public function generateField($key = '', $type = '', $length = 11, $default = '', $comment = '主键', $is_auto_increment = false){
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}";
$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;
}
@@ -96,31 +114,27 @@ class Datatable {
*/
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['name'] = $attr['name'];
$field_attr['type'] = $attr['type'] ? $this->typeAlist[$attr['type']] : 'varchar';
if (intval($attr['length']) && $attr['length']) {
$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'] = $field_attr['default'];
} 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['is_null'] = $attr['is_must'] ? 'NOT NULL' : 'NULL';
$field_attr['default'] = $attr['value'] != '' ? 'DEFAULT "' . $attr['default'] . '"' : 'DEFAULT null';
$field_attr['comment'] = (isset($attr['remark']) && $attr['remark']) ? $attr['remark'] : $attr['title'];
$field_attr['after'] = (isset($attr['after']) && $attr['after']) ? ' AFTER `' . $attr['after'] . '`' : ' AFTER `id`';
$field_attr['action'] = (isset($attr['action']) && $attr['action']) ? $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']}'";
$this->sql = "ALTER TABLE `{$field_attr['table']}` ADD `{$field_attr['name']}` {$field_attr['type']}{$field_attr['length']} {$field_attr['is_null']} {$field_attr['default']} COMMENT '{$field_attr['comment']}' {$field_attr['after']}";
} 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']}'";
$field_attr['oldname'] = (isset($attr['oldname']) && $attr['oldname']) ? $attr['oldname'] : '';
$this->sql = "ALTER TABLE `{$field_attr['table']}` CHANGE `{$field_attr['oldname']}` `{$field_attr['name']}` {$field_attr['type']}{$field_attr['length']} {$field_attr['is_null']} {$field_attr['default']} COMMENT '{$field_attr['comment']}'";
}
return $this;
}