控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ModelField.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 字段模型
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\model\cms;
  15. use app\common\model\Modelbase;
  16. use think\Db;
  17. /**
  18. * 字段模型
  19. */
  20. class ModelField extends Modelbase
  21. {
  22. protected $autoWriteTimestamp = true;
  23. protected $insert = ['status' => 1];
  24. protected $ext_table = '_data';
  25. //添加字段
  26. public function addField($data = null)
  27. {
  28. $data['name'] = strtolower($data['name']);
  29. $data['ifsystem'] = isset($data['ifsystem']) ? intval($data['ifsystem']) : 0;
  30. //模型id
  31. $modelid = $data['modelid'];
  32. //完整表名获取 判断主表 还是副表
  33. $tablename = $this->getModelTableName($modelid, $data['ifsystem']);
  34. if (!$this->table_exists($tablename)) {
  35. throw new \Exception('数据表不存在!');
  36. }
  37. $tablename = config('database.prefix') . $tablename;
  38. //判断字段名唯一性
  39. if ($this->where('name', $data['name'])->where('modelid', $modelid)->value('id')) {
  40. throw new \Exception("字段'" . $data['name'] . "`已经存在");
  41. }
  42. $data['isadd'] = isset($data['isadd']) ? intval($data['isadd']) : 0;
  43. $data['ifrequire'] = isset($data['ifrequire']) ? intval($data['ifrequire']) : 0;
  44. if ($data['ifrequire'] && !$data['isadd']) {
  45. throw new \Exception('必填字段不可以隐藏!');
  46. }
  47. if ($data['setting']['value'] === '') {
  48. $default = '';
  49. } elseif (strstr(strtolower($data['setting']['define']), 'text') || strstr(strtolower($data['setting']['define']), 'blob')) {
  50. $default = '';
  51. } else {
  52. $default = " DEFAULT '{$data['setting']['value']}'";
  53. }
  54. //先将字段存在设置的主表或附表里面 再将数据存入ModelField
  55. $sql = <<<EOF
  56. ALTER TABLE `{$tablename}`
  57. ADD COLUMN `{$data['name']}` {$data['setting']['define']} {$default} COMMENT '{$data['title']}';
  58. EOF;
  59. Db::execute($sql);
  60. $fieldInfo = Db::name('field_type')->where('name', $data['type'])->field('ifoption,ifstring')->find();
  61. //只有主表文本类字段才可支持搜索
  62. $data['ifsearch'] = isset($data['ifsearch']) ? ($fieldInfo['ifstring'] && $data['ifsystem'] ? intval($data['ifsearch']) : 0) : 0;
  63. $data['status'] = isset($data['status']) ? intval($data['status']) : 0;
  64. $data['iffixed'] = 0;
  65. $data['setting']['options'] = $fieldInfo['ifoption'] ? $data['setting']['options'] : '';
  66. //附加属性值
  67. $data['setting'] = serialize($data['setting']);
  68. $fieldid = self::create($data, true);
  69. if ($fieldid) {
  70. //清理缓存
  71. cache('ModelField', null);
  72. return true;
  73. } else {
  74. //回滚
  75. Db::execute("ALTER TABLE `{$tablename}` DROP `{$data['name']}`");
  76. throw new \Exception('字段信息入库失败!');
  77. }
  78. return true;
  79. }
  80. /**
  81. * 编辑字段
  82. * @param type $data 编辑字段数据
  83. * @param type $fieldid 字段id
  84. * @return boolean
  85. */
  86. public function editField($data, $fieldid = 0)
  87. {
  88. $data['name'] = strtolower($data['name']);
  89. $data['ifsystem'] = isset($data['ifsystem']) ? intval($data['ifsystem']) : 0;
  90. if (!$fieldid && !isset($data['fieldid'])) {
  91. throw new \Exception('缺少字段id!');
  92. } else {
  93. $fieldid = $fieldid ? $fieldid : (int) $data['fieldid'];
  94. }
  95. //原字段信息
  96. $info = self::where(array("id" => $fieldid))->find();
  97. if (empty($info)) {
  98. throw new \Exception('该字段不存在!');
  99. }
  100. //模型id
  101. $data['modelid'] = $modelid = $info['modelid'];
  102. //完整表名获取 判断主表 还是副表
  103. $tablename = $this->getModelTableName($modelid, $data['ifsystem']);
  104. if (!$this->table_exists($tablename)) {
  105. throw new \Exception('数据表不存在!');
  106. }
  107. $tablename = config('database.prefix') . $tablename;
  108. //判断字段名唯一性
  109. if ($this->where('name', $data['name'])->where('modelid', $modelid)->where('id', '<>', $fieldid)->value('id')) {
  110. throw new \Exception("字段'" . $data['name'] . "`已经存在");
  111. }
  112. $data['isadd'] = isset($data['isadd']) ? intval($data['isadd']) : 0;
  113. $data['ifrequire'] = isset($data['ifrequire']) ? intval($data['ifrequire']) : 0;
  114. if ($data['ifrequire'] && !$data['isadd']) {
  115. throw new \Exception('必填字段不可以隐藏!');
  116. }
  117. if ($data['setting']['value'] === '') {
  118. $default = '';
  119. } elseif (strstr(strtolower($data['setting']['define']), 'text') || strstr(strtolower($data['setting']['define']), 'blob')) {
  120. $default = '';
  121. } else {
  122. $default = " DEFAULT '{$data['setting']['value']}'";
  123. }
  124. $sql = <<<EOF
  125. ALTER TABLE `{$tablename}`
  126. CHANGE COLUMN `{$info['name']}` `{$data['name']}` {$data['setting']['define']} {$default} COMMENT '{$data['title']}';
  127. EOF;
  128. try {
  129. Db::execute($sql);
  130. } catch (\Exception $e) {
  131. throw new \Exception($e->getMessage());
  132. }
  133. $fieldInfo = Db::name('field_type')->where('name', $data['type'])->field('ifoption,ifstring')->find();
  134. //只有主表文本类字段才可支持搜索
  135. $data['ifsearch'] = isset($data['ifsearch']) ? ($fieldInfo['ifstring'] && $data['ifsystem'] ? intval($data['ifsearch']) : 0) : 0;
  136. $data['status'] = isset($data['status']) ? intval($data['status']) : 0;
  137. //$data['options'] = $fieldInfo['ifoption'] ? $data['options'] : '';
  138. $data['setting']['options'] = $fieldInfo['ifoption'] ? $data['setting']['options'] : '';
  139. //附加属性值
  140. $data['setting'] = serialize($data['setting']);
  141. //清理缓存
  142. cache('ModelField', null);
  143. self::update($data, ['id' => $fieldid], true);
  144. return true;
  145. }
  146. /**
  147. * 删除字段
  148. * @param type $fieldid 字段id
  149. * @return boolean
  150. */
  151. public function deleteField($fieldid)
  152. {
  153. //原字段信息
  154. $info = self::where(array("id" => $fieldid))->find();
  155. if (empty($info)) {
  156. throw new \Exception('该字段不存在!');
  157. }
  158. //模型id
  159. $modelid = $info['modelid'];
  160. //完整表名获取 判断主表 还是副表
  161. $tablename = $this->getModelTableName($modelid, $info['ifsystem']);
  162. if (!$this->table_exists($tablename)) {
  163. throw new \Exception('数据表不存在!');
  164. }
  165. $tablename = config('database.prefix') . $tablename;
  166. //判断是否允许删除
  167. $sql = <<<EOF
  168. ALTER TABLE `{$tablename}`
  169. DROP COLUMN `{$info['name']}`;
  170. EOF;
  171. Db::execute($sql);
  172. self::get($fieldid)->delete();
  173. return true;
  174. }
  175. /**
  176. * 根据模型ID,返回表名
  177. * @param type $modelid
  178. * @param type $modelid
  179. * @return string
  180. */
  181. protected function getModelTableName($modelid, $ifsystem = 1)
  182. {
  183. //读取模型配置 以后优化缓存形式
  184. $model_cache = cache("Model");
  185. //表名获取
  186. $model_table = $model_cache[$modelid]['tablename'];
  187. //完整表名获取 判断主表 还是副表
  188. $tablename = $ifsystem ? $model_table : $model_table . "_data";
  189. return $tablename;
  190. }
  191. }