Model.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\cms\model;
  10. use think\Model as ThinkModel;
  11. use think\Db;
  12. /**
  13. * 内容模型
  14. * @package app\cms\model
  15. */
  16. class Model extends ThinkModel
  17. {
  18. // 设置当前模型对应的完整数据表名称
  19. protected $name = 'cms_model';
  20. // 自动写入时间戳
  21. protected $autoWriteTimestamp = true;
  22. /**
  23. * 获取内容模型列表
  24. * @author 蔡伟明 <314013107@qq.com>
  25. * @return array|mixed
  26. */
  27. public static function getList()
  28. {
  29. $data_list = cache('cms_model_list');
  30. if (!$data_list) {
  31. $data_list = self::where('status', 1)->column(true, 'id');
  32. // 非开发模式,缓存数据
  33. if (config('develop_mode') == 0) {
  34. cache('cms_model_list', $data_list);
  35. }
  36. }
  37. return $data_list;
  38. }
  39. /**
  40. * 获取内容模型标题列表(只含id和title)
  41. * @param array $map 筛选条件
  42. * @author 蔡伟明 <314013107@qq.com>
  43. * @return array|mixed
  44. */
  45. public static function getTitleList($map = [])
  46. {
  47. return self::where('status', 1)->where($map)->column('id,title');
  48. }
  49. /**
  50. * 删除附加表
  51. * @param null $model 内容模型id
  52. * @author 蔡伟明 <314013107@qq.com>
  53. * @return bool
  54. */
  55. public static function deleteTable($model = null)
  56. {
  57. if ($model === null) {
  58. return false;
  59. }
  60. $table_name = self::where('id', $model)->value('table');
  61. return false !== Db::execute("DROP TABLE IF EXISTS `{$table_name}`");
  62. }
  63. /**
  64. * 创建独立模型表
  65. * @param mixed $data 模型数据
  66. * @author 蔡伟明 <314013107@qq.com>
  67. * @return bool
  68. */
  69. public static function createTable($data)
  70. {
  71. if ($data['type'] == 2) {
  72. // 新建独立扩展表
  73. $sql = <<<EOF
  74. CREATE TABLE IF NOT EXISTS `{$data['table']}` (
  75. `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '文档id' ,
  76. `cid` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '栏目id' ,
  77. `uid` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户id' ,
  78. `model` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '模型id' ,
  79. `title` varchar(256) NOT NULL DEFAULT '' COMMENT '标题' ,
  80. `create_time` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间' ,
  81. `update_time` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新时间' ,
  82. `sort` int(11) NOT NULL DEFAULT 100 COMMENT '排序' ,
  83. `status` tinyint(2) UNSIGNED NOT NULL DEFAULT 0 COMMENT '状态' ,
  84. `view` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '点击量' ,
  85. `trash` tinyint(2) UNSIGNED NOT NULL DEFAULT 0 COMMENT '回收站' ,
  86. PRIMARY KEY (`id`)
  87. )
  88. ENGINE=MyISAM
  89. DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
  90. CHECKSUM=0
  91. ROW_FORMAT=DYNAMIC
  92. DELAY_KEY_WRITE=0
  93. COMMENT='{$data['title']}模型表'
  94. ;
  95. EOF;
  96. } else {
  97. // 新建普通扩展表
  98. $sql = <<<EOF
  99. CREATE TABLE IF NOT EXISTS `{$data['table']}` (
  100. `aid` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '文档id' ,
  101. PRIMARY KEY (`aid`)
  102. )
  103. ENGINE=MyISAM
  104. DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
  105. CHECKSUM=0
  106. ROW_FORMAT=DYNAMIC
  107. DELAY_KEY_WRITE=0
  108. COMMENT='{$data['title']}模型扩展表'
  109. ;
  110. EOF;
  111. }
  112. try {
  113. Db::execute($sql);
  114. } catch(\Exception $e) {
  115. return false;
  116. }
  117. if ($data['type'] == 2) {
  118. // 添加默认字段
  119. $default = [
  120. 'model' => $data['id'],
  121. 'level' => '',
  122. 'create_time' => request()->time(),
  123. 'update_time' => request()->time(),
  124. 'status' => 1
  125. ];
  126. $data = [
  127. [
  128. 'name' => 'id',
  129. 'title' => '文档id',
  130. 'define' => 'int(11) UNSIGNED NOT NULL',
  131. 'type' => 'text',
  132. 'show' => 0
  133. ],
  134. [
  135. 'name' => 'cid',
  136. 'title' => '栏目',
  137. 'define' => 'int(11) UNSIGNED NOT NULL',
  138. 'type' => 'static',
  139. 'show' => 0,
  140. 'value' => 0,
  141. ],
  142. [
  143. 'name' => 'uid',
  144. 'title' => '用户id',
  145. 'define' => 'int(11) UNSIGNED NOT NULL',
  146. 'type' => 'text',
  147. 'show' => 0,
  148. 'value' => 0,
  149. ],
  150. [
  151. 'name' => 'model',
  152. 'title' => '文档模型',
  153. 'define' => 'int(11) UNSIGNED NOT NULL',
  154. 'type' => 'text',
  155. 'show' => 0,
  156. 'value' => 0,
  157. ],
  158. [
  159. 'name' => 'title',
  160. 'title' => '标题',
  161. 'define' => 'varchar(256) NOT NULL',
  162. 'type' => 'text',
  163. 'show' => 1
  164. ],
  165. [
  166. 'name' => 'create_time',
  167. 'title' => '创建时间',
  168. 'define' => 'int(11) UNSIGNED NOT NULL',
  169. 'type' => 'datetime',
  170. 'show' => 0,
  171. 'value' => 0,
  172. ],
  173. [
  174. 'name' => 'update_time',
  175. 'title' => '更新时间',
  176. 'define' => 'int(11) UNSIGNED NOT NULL',
  177. 'type' => 'datetime',
  178. 'show' => 0,
  179. 'value' => 0,
  180. ],
  181. [
  182. 'name' => 'sort',
  183. 'title' => '排序',
  184. 'define' => 'int(11) UNSIGNED NOT NULL',
  185. 'type' => 'text',
  186. 'show' => 1,
  187. 'value' => 100,
  188. ],
  189. [
  190. 'name' => 'status',
  191. 'title' => '状态',
  192. 'define' => 'tinyint(2) NOT NULL',
  193. 'type' => 'radio',
  194. 'show' => 1,
  195. 'value' => 1,
  196. 'options' => '0:禁用
  197. 1:启用'
  198. ],
  199. [
  200. 'name' => 'view',
  201. 'title' => '点击量',
  202. 'define' => 'int(11) UNSIGNED NOT NULL',
  203. 'type' => 'text',
  204. 'show' => 0,
  205. 'value' => 0
  206. ],
  207. [
  208. 'name' => 'trash',
  209. 'title' => '回收站',
  210. 'define' => 'tinyint(2) NOT NULL',
  211. 'type' => 'radio',
  212. 'show' => 0,
  213. 'value' => 0
  214. ]
  215. ];
  216. foreach ($data as $item) {
  217. $item = array_merge($item, $default);
  218. Db::name('cms_field')->insert($item);
  219. }
  220. }
  221. return true;
  222. }
  223. }