更新功能

This commit is contained in:
2020-03-27 19:01:10 +08:00
parent 02b17e0da8
commit f239647c7c
10 changed files with 164 additions and 137 deletions

View File

@@ -21,6 +21,16 @@ class Category extends \think\Model{
'icon' => 'integer',
);
public static function getCategoryTree($map = []){
$list = self::where($map)->select();
if (!empty($list)) {
$tree = new \sent\tree\Tree();
$list = $tree->toFormatTree($list->toArray());
}
return $list;
}
public function change() {
$data = input('post.');
if ($data['id']) {

View File

@@ -12,10 +12,9 @@ namespace app\model;
/**
* 设置模型
*/
class ContentModel {
class Content extends Model {
protected $auto = ['update_time'];
protected $insert = ['create_time', 'status' => 1];
protected $type = array(
'create_time' => 'integer',
'update_time' => 'integer',
);
}

View File

@@ -15,64 +15,60 @@ namespace app\model;
class Model extends \think\Model{
protected $auto = ['update_time'];
protected $insert = ['name', 'create_time', 'status' => 1, 'list_grid' => "id:ID\r\ntitle:标题\r\ncreate_time:添加时间|time_format\r\nupdate_time:更新时间|time_format"];
protected $insert = ['name', 'create_time', 'status' => 1, 'list_grid' => "id:ID\r\ntitle:标题\r\ncreate_time:添加时间\r\nupdate_time:更新时间"];
protected $type = array(
'id' => 'integer',
'create_time' => 'integer',
'update_time' => 'integer',
'id' => 'integer'
);
// protected static function init() {
// self::beforeInsert(function ($event) {
// $data = $event->toArray();
// $tablename = strtolower($data['name']);
// //实例化一个数据库操作类
// $db = new \com\Datatable();
// //检查表是否存在并创建
// if (!$db->CheckTable($tablename)) {
// //创建新表
// return $db->initTable($tablename, $data['title'], 'id')->query();
// } else {
// return false;
// }
// });
// self::afterInsert(function ($event) {
// $data = $event->toArray();
protected static function onAfterInsert($model){
$data = $model->getDate();
// $fields = include (APP_PATH . 'admin/fields.php');
// if (!empty($fields)) {
// foreach ($fields as $key => $value) {
// if ($data['is_doc']) {
// $fields[$key]['model_id'] = $data['id'];
// } else {
// if (in_array($key, array('uid', 'status', 'view', 'create_time', 'update_time'))) {
// $fields[$key]['model_id'] = $data['id'];
// } else {
// unset($fields[$key]);
// }
// }
// }
// model('Attribute')->saveAll($fields);
// }
// return true;
// });
// self::beforeUpdate(function ($event) {
// $data = $event->toArray();
// if (isset($data['attribute_sort']) && $data['attribute_sort']) {
// $attribute_sort = json_decode($data['attribute_sort'], true);
$fields = [
'title'=> ['name' => 'title', 'title' => '标题', 'type' => 'text', 'length' => 200, 'extra' => '', 'remark' => '标题', 'is_show' => 1, 'is_must' => 1, 'value'=>''],
'category_id' => ['name' => 'category_id', 'title' => '栏目', 'type' => 'bind', 'length' => 10, 'extra' => 'category', 'remark' => '栏目', 'is_show' => 1, 'is_must' => 1, 'value'=>'0'],
'uid' => ['name' => 'uid', 'title' => '用户UID', 'type' => 'num', 'length' => 11, 'extra' => '', 'remark' => '用户UID', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'],
'cover_id' => ['name' => 'cover_id', 'title' => '内容封面', 'type' => 'image', 'length' => 10, 'extra' => '', 'remark' => '内容封面', 'is_show' => 1, 'is_must' => 0, 'value'=>''],
'description' => ['name' => 'description', 'title' => '内容描述', 'type' => 'textarea', 'length' => '', 'extra' => '', 'remark' => '内容描述', 'is_show' => 1, 'is_must' => 0, 'value'=>''],
'content' => ['name' => 'content', 'title' => '内容', 'type' => 'editor', 'length' => '', 'extra' => '', 'remark' => '内容', 'is_show' => 1, 'is_must' => 0, 'value'=>''],
'status' => ['name' => 'status', 'title' => '数据状态', 'type' => 'select', 'length' => 2, 'extra' => "-1:删除\r\n0:禁用\r\n1:正常\r\n2:待审核\r\n3:草稿", 'remark' => '数据状态', 'is_show' => 1, 'is_must' => 1, 'value'=>'1'],
'is_top' => ['name' => 'is_top', 'title' => '是否置顶', 'type' => 'bool', 'length' => 2, 'extra' => '', 'remark' => '是否置顶', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'],
'view' => ['name' => 'view', 'title' => '浏览数量', 'type' => 'num', 'length' => 11, 'extra' => '', 'remark' => '浏览数量', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'],
'update_time' => ['name' => 'update_time', 'title' => '更新时间', 'type' => 'datetime', 'length' => 11, 'extra' => '', 'remark' => '更新时间', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'],
'create_time' => ['name' => 'create_time', 'title' => '添加时间', 'type' => 'datetime', 'length' => 11, 'extra' => '', 'remark' => '添加时间', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'],
];
if (!empty($fields)) {
foreach ($fields as $key => $value) {
if ($data['is_doc']) {
$fields[$key]['model_id'] = $data['id'];
} else {
if (in_array($key, array('uid', 'status', 'view', 'create_time', 'update_time'))) {
$fields[$key]['model_id'] = $data['id'];
} else {
unset($fields[$key]);
}
}
}
(new Attribute())->saveAll($fields);
}
return 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 static function onAfterUpdate($model){
$data = $model->getDate();
if (isset($data['attribute_sort']) && $data['attribute_sort']) {
$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) : '';