控制台应用,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.

Models.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\formguide;
  15. use think\Db;
  16. use think\Exception;
  17. use think\facade\Cache;
  18. use think\facade\Config;
  19. use think\Model;
  20. class Models extends Model
  21. {
  22. protected $name = 'model';
  23. protected $autoWriteTimestamp = true;
  24. protected static function init()
  25. {
  26. //添加
  27. self::beforeInsert(function ($row) {
  28. $setting['forward'] = $row->forward;
  29. $setting['mails'] = $row->mails;
  30. $setting['interval'] = $row->interval;
  31. $setting['allowmultisubmit'] = $row->allowmultisubmit;
  32. $setting['allowunreg'] = $row->allowunreg;
  33. $setting['isverify'] = $row->isverify;
  34. $setting['show_template'] = $row->show_template;
  35. $row['tablename'] = 'formguide_' . $row['tablename'];
  36. $row['setting'] = serialize($setting);
  37. $row['module'] = 'formguide';
  38. $info = null;
  39. try {
  40. $info = Db::name($row['tablename'])->getPk();
  41. } catch (\Exception $e) {
  42. }
  43. if ($info) {
  44. throw new Exception("数据表已经存在");
  45. }
  46. });
  47. self::afterInsert(function ($row) {
  48. cache::set("Model", null);
  49. cache::set('ModelField', null);
  50. $prefix = Config::get("database.prefix");
  51. //创建模型表
  52. $sql = "CREATE TABLE `{$prefix}{$row['tablename']}` (
  53. `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  54. `user_id` int(10) DEFAULT NULL COMMENT '会员ID',
  55. `username` varchar(20) NOT NULL,
  56. `create_time` int(10) unsigned DEFAULT NULL COMMENT '添加时间',
  57. `ip` char(15) NOT NULL DEFAULT '',
  58. PRIMARY KEY (`id`)
  59. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;";
  60. Db::execute($sql);
  61. });
  62. //编辑
  63. self::beforeUpdate(function ($row) {
  64. unset($row['type'], $row['tablename']);
  65. $changedData = $row->getChangedData();
  66. $setting = [];
  67. if (isset($changedData['forward'])) {
  68. $setting['forward'] = $row->forward;
  69. }
  70. if (isset($changedData['mails'])) {
  71. $setting['mails'] = $row->mails;
  72. }
  73. if (isset($changedData['interval'])) {
  74. $setting['interval'] = $row->interval;
  75. }
  76. if (isset($changedData['allowmultisubmit'])) {
  77. $setting['allowmultisubmit'] = $row->allowmultisubmit;
  78. }
  79. if (isset($changedData['allowunreg'])) {
  80. $setting['allowunreg'] = $row->allowunreg;
  81. }
  82. if (isset($changedData['isverify'])) {
  83. $setting['isverify'] = $row->isverify;
  84. }
  85. if (isset($changedData['show_template'])) {
  86. $setting['show_template'] = $row->show_template;
  87. }
  88. if ($setting) {
  89. $row['setting'] = serialize($setting);
  90. }
  91. });
  92. self::afterUpdate(function ($row) {
  93. //更新缓存
  94. cache::set("Model", null);
  95. cache::set('ModelField', null);
  96. });
  97. //删除
  98. self::afterDelete(function ($row) {
  99. cache::set("Model", null);
  100. cache::set('ModelField', null);
  101. //删除所有和这个模型相关的字段
  102. Db::name("ModelField")->where("modelid", $row['id'])->delete();
  103. //删除主表
  104. $prefix = Config::get("database.prefix");
  105. Db::execute("DROP TABLE IF EXISTS `{$prefix}{$row['tablename']}`");
  106. });
  107. }
  108. public function getSettingAttr($value, $data)
  109. {
  110. return unserialize($value);
  111. }
  112. }