Test.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. /**
  5. * Test
  6. */
  7. class Test extends Model
  8. {
  9. // 表名
  10. protected $name = 'test';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = true;
  13. // 追加属性
  14. protected $append = [
  15. 'city_text',
  16. ];
  17. // 字段类型转换
  18. protected $type = [
  19. 'array' => 'json',
  20. 'timestamp' => 'timestamp:Y-m-d H:i:s',
  21. ];
  22. protected static function onAfterInsert($model)
  23. {
  24. if ($model->weigh == 0) {
  25. $pk = $model->getPk();
  26. if (strlen($model[$pk]) >= 19) {
  27. $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
  28. } else {
  29. $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
  30. }
  31. }
  32. }
  33. public function getImagesAttr($value): array
  34. {
  35. if ($value === '' || $value === null) return [];
  36. if (!is_array($value)) {
  37. return explode(',', $value);
  38. }
  39. return $value;
  40. }
  41. public function setImagesAttr($value): string
  42. {
  43. return is_array($value) ? implode(',', $value) : $value;
  44. }
  45. public function getFilesAttr($value): array
  46. {
  47. if ($value === '' || $value === null) return [];
  48. if (!is_array($value)) {
  49. return explode(',', $value);
  50. }
  51. return $value;
  52. }
  53. public function setFilesAttr($value): string
  54. {
  55. return is_array($value) ? implode(',', $value) : $value;
  56. }
  57. public function getCheckboxAttr($value): array
  58. {
  59. if ($value === '' || $value === null) return [];
  60. if (!is_array($value)) {
  61. return explode(',', $value);
  62. }
  63. return $value;
  64. }
  65. public function setCheckboxAttr($value): string
  66. {
  67. return is_array($value) ? implode(',', $value) : $value;
  68. }
  69. public function getSelectsAttr($value): array
  70. {
  71. if ($value === '' || $value === null) return [];
  72. if (!is_array($value)) {
  73. return explode(',', $value);
  74. }
  75. return $value;
  76. }
  77. public function setSelectsAttr($value): string
  78. {
  79. return is_array($value) ? implode(',', $value) : $value;
  80. }
  81. public function getFloatAttr($value): float
  82. {
  83. return (float)$value;
  84. }
  85. public function getCityAttr($value): array
  86. {
  87. if ($value === '' || $value === null) return [];
  88. if (!is_array($value)) {
  89. return explode(',', $value);
  90. }
  91. return $value;
  92. }
  93. public function setCityAttr($value): string
  94. {
  95. return is_array($value) ? implode(',', $value) : $value;
  96. }
  97. public function getCityTextAttr($value, $row): string
  98. {
  99. if ($row['city'] === '' || $row['city'] === null) return '';
  100. $cityNames = \think\facade\Db::name('area')->whereIn('id', $row['city'])->column('name');
  101. return $cityNames ? implode(',', $cityNames) : '';
  102. }
  103. public function getArrayAttr($value): array
  104. {
  105. return !$value ? [] : json_decode($value, true);
  106. }
  107. public function setTimeAttr($value): string
  108. {
  109. return $value ? date('H:i:s', strtotime($value)) : '';
  110. }
  111. public function getEditorAttr($value): string
  112. {
  113. return !$value ? '' : htmlspecialchars_decode($value);
  114. }
  115. }