Field.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 Field extends ThinkModel
  17. {
  18. // 设置当前模型对应的完整数据表名称
  19. protected $name = 'cms_field';
  20. // 自动写入时间戳
  21. protected $autoWriteTimestamp = true;
  22. // 当前表名
  23. protected $_table_name = '';
  24. /**
  25. * 创建字段
  26. * @param null $field 字段数据
  27. * @author 蔡伟明 <314013107@qq.com>
  28. * @return bool
  29. */
  30. public function newField($field = null)
  31. {
  32. if ($field === null) {
  33. $this->error = '缺少参数';
  34. return false;
  35. }
  36. if ($this->tableExist($field['model'])) {
  37. $sql = <<<EOF
  38. ALTER TABLE `{$this->_table_name}`
  39. ADD COLUMN `{$field['name']}` {$field['define']} COMMENT '{$field['title']}';
  40. EOF;
  41. } else {
  42. $mdoel_title = get_model_title($field['model']);
  43. // 新建普通扩展表
  44. $sql = <<<EOF
  45. CREATE TABLE IF NOT EXISTS `{$this->_table_name}` (
  46. `aid` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '文档id' ,
  47. `{$field['name']}` {$field['define']} COMMENT '{$field['title']}' ,
  48. PRIMARY KEY (`aid`)
  49. )
  50. ENGINE=MyISAM
  51. DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
  52. CHECKSUM=0
  53. ROW_FORMAT=DYNAMIC
  54. DELAY_KEY_WRITE=0
  55. COMMENT='{$mdoel_title}模型扩展表'
  56. ;
  57. EOF;
  58. }
  59. try {
  60. Db::execute($sql);
  61. } catch(\Exception $e) {
  62. $this->error = '字段添加失败';
  63. return false;
  64. }
  65. return true;
  66. }
  67. /**
  68. * 更新字段
  69. * @param null $field 字段数据
  70. * @author 蔡伟明 <314013107@qq.com>
  71. * @return bool
  72. */
  73. public function updateField($field = null)
  74. {
  75. if ($field === null) {
  76. return false;
  77. }
  78. // 获取原字段名
  79. $field_name = $this->where('id', $field['id'])->value('name');
  80. if ($this->tableExist($field['model'])) {
  81. $sql = <<<EOF
  82. ALTER TABLE `{$this->_table_name}`
  83. CHANGE COLUMN `{$field_name}` `{$field['name']}` {$field['define']} COMMENT '{$field['title']}';
  84. EOF;
  85. try {
  86. Db::execute($sql);
  87. } catch(\Exception $e) {
  88. return false;
  89. }
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. /**
  96. * 删除字段
  97. * @param null $field 字段数据
  98. * @author 蔡伟明 <314013107@qq.com>
  99. * @return bool
  100. */
  101. public function deleteField($field = null)
  102. {
  103. if ($field === null) {
  104. return false;
  105. }
  106. if ($this->tableExist($field['model'])) {
  107. $sql = <<<EOF
  108. ALTER TABLE `{$this->_table_name}`
  109. DROP COLUMN `{$field['name']}`;
  110. EOF;
  111. try {
  112. Db::execute($sql);
  113. } catch(\Exception $e) {
  114. return false;
  115. }
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. }
  121. /**
  122. * 检查表是否存在
  123. * @param string $model 文档模型id
  124. * @author 蔡伟明 <314013107@qq.com>
  125. * @return bool
  126. */
  127. private function tableExist($model = '')
  128. {
  129. $this->_table_name = strtolower(get_model_table($model));
  130. return true == Db::query("SHOW TABLES LIKE '{$this->_table_name}'");
  131. }
  132. }