重新定义模型

This commit is contained in:
2017-06-11 22:28:37 +08:00
parent fa979a5382
commit ac1e6a634f
19 changed files with 465 additions and 434 deletions

View File

@@ -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'];

View File

@@ -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();

View File

@@ -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();
};
}
}

View File

@@ -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;
}

View File

@@ -29,5 +29,4 @@ class Model extends Base{
'add' => 'name,title',
'edit' => 'title',
);
}