From 047e41a8d9f6d547b81cbf0dd407cb6366f20ec3 Mon Sep 17 00:00:00 2001 From: molong Date: Fri, 2 Mar 2018 08:58:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/common/model/Attribute.php | 105 +++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 application/common/model/Attribute.php diff --git a/application/common/model/Attribute.php b/application/common/model/Attribute.php new file mode 100644 index 00000000..0b700edb --- /dev/null +++ b/application/common/model/Attribute.php @@ -0,0 +1,105 @@ + +// +---------------------------------------------------------------------- + +namespace app\common\model; + +/** +* 设置模型 +*/ +class Attribute extends Base{ + + protected $type = array( + '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('name', '<>', $data['name'])->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']]); + return $type_text[0]; + } + + public function getFieldlist($map,$index='id'){ + $list = array(); + $row = db('Attribute')->field('*,remark as help,type,extra as "option",model_id')->where($map)->order('group_id asc, sort asc')->select(); + foreach ($row as $key => $value) { + if (in_array($value['type'],array('checkbox','radio','select','bool'))) { + $value['option'] = parse_field_attr($value['extra']); + } elseif ($value['type'] == 'bind') { + $extra = parse_field_bind($value['extra'], '', $value['model_id']); + $option = array(); + foreach ($extra as $k => $v) { + $option[$v['id']] = $v['title_show']; + } + $value['option'] = $option; + } + $list[$value['id']] = $value; + } + return $list; + } + + public function del($id, $model_id){ + $map['id'] = $id; + $info = $this->find($id); + $tablename = db('Model')->where(array('id'=>$model_id))->value('name'); + + //先删除字段表内的数据 + $result = $this->where($map)->delete(); + if ($result) { + $tablename = strtolower($tablename); + //删除模型表中字段 + $db = new \com\Datatable(); + if (!$db->CheckField($tablename,$info['name'])) { + return true; + } + $result = $db->delField($tablename,$info['name'])->query(); + if ($result) { + return true; + }else{ + $this->error = "删除失败!"; + return false; + } + }else{ + $this->error = "删除失败!"; + return false; + } + } +} \ No newline at end of file