模型基本雏形
This commit is contained in:
@@ -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();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
|
||||
@@ -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',
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user