Fields::getContributeFields(), 'publishfields' => Fields::getPublishFields(), ]; $row['setting'] = json_encode($setting); if (!preg_match("/^([a-z0-9_]+)$/", $row['table'])) { throw new Exception("表名只支持小写字母、数字、下划线"); } $exist = Modelx::where('table', $row['table'])->find(); if ($exist) { throw new Exception("已经存在相同表名的模型"); } $info = null; try { $info = Db::name($row['table'])->getTableInfo(); } catch (\Exception $e) { } if ($info) { throw new Exception("数据表已经存在"); } }); self::afterInsert(function ($row) { $prefix = Config::get('database.prefix'); $sql = "CREATE TABLE `{$prefix}{$row['table']}` (`id` int(10) NOT NULL,`content` longtext NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='{$row['name']}'"; try { Db::execute($sql); } catch (\Exception $e) { throw new Exception("发生错误:" . $e->getMessage()); } }); //存在栏目无法删除 self::beforeDelete(function ($row) { $exist = Channel::where('model_id', $row['id'])->find(); if ($exist) { throw new Exception("模型下存在栏目,无法进行删除"); } }); //删除模型后删除对应的表字段 self::afterDelete(function ($row) { Db::name("cms_fields")->where(['source' => 'model', 'source_id' => $row['id']])->delete(); }); } public function getFieldsAttr($value, $data) { return is_array($value) ? $value : ($value ? explode(',', $value) : []); } public function getSettingAttr($value, $data) { return is_array($value) ? $value : (array)json_decode($data['setting'], true); } public function setSettingAttr($value) { return is_array($value) ? json_encode($value) : $value; } }