BoardMeta.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Cache;
  5. class BoardMeta extends Model
  6. {
  7. protected $table = 'tb_board_meta';
  8. protected $primaryKey = 'board_id';
  9. public $keyType = 'int';
  10. public $incrementing = false;
  11. public $timestamps = false;
  12. public string $metaKey = 'key';
  13. public string $metaValue = 'value';
  14. public string $cachePrefix = 'board-meta-';
  15. public function __set($name, $value)
  16. {
  17. $this->{$name} = $value;
  18. }
  19. public function __get($name)
  20. {
  21. return ($this->{$name} ?? null);
  22. }
  23. /**
  24. * 게시판 설정 정보 조회
  25. */
  26. public function data($boardID = 0): BoardMeta|null
  27. {
  28. $res = $this->where($this->primaryKey, $boardID)->get();
  29. if ($res->count() > 0) {
  30. foreach ($res as $val) {
  31. $this->{$val[$this->metaKey]} = $val[$this->metaValue];
  32. }
  33. return $this;
  34. }
  35. return null;
  36. }
  37. /**
  38. * 캐시된 설정 값 조회
  39. */
  40. public function getAllMeta($boardID = 0): ?BoardMeta
  41. {
  42. $cacheName = ($this->cachePrefix . $boardID);
  43. if (!$ret = Cache::get($cacheName)) {
  44. $ret = $this->data($boardID);
  45. Cache::forever($cacheName, $ret);
  46. }
  47. return $ret;
  48. }
  49. /**
  50. * 저장
  51. */
  52. public function save($boardID = 0, $saveData = []): void
  53. {
  54. if ($saveData && is_array($saveData)) {
  55. foreach ($saveData as $column => $value) {
  56. $this->_metaUpdate($boardID, $column, $value);
  57. }
  58. }
  59. Cache::forget($this->cachePrefix . $boardID);
  60. $this->getAllMeta($boardID);
  61. }
  62. /**
  63. * 삭제
  64. */
  65. public function deleteMeta($boardID = 0): void
  66. {
  67. $this->where($this->primaryKey, $boardID)->delete();
  68. Cache::forget($this->cachePrefix . $boardID);
  69. }
  70. /**
  71. * 조회
  72. */
  73. public function item($column = '', $default = null): mixed
  74. {
  75. if (empty($column)) {
  76. return false;
  77. }
  78. $return = $default;
  79. if (property_exists($this, $column)) {
  80. $return = $this->{$column};
  81. }
  82. return $return;
  83. }
  84. /**
  85. * 등록/수정 처리
  86. */
  87. private function _metaUpdate($boardID = 0, $column = '', $value = null): mixed
  88. {
  89. if(!$boardID) {
  90. return false;
  91. }
  92. $column = trim($column);
  93. if (empty($column)) {
  94. return false;
  95. }
  96. $items = $this->data($boardID);
  97. if($items) {
  98. $oldValue = property_exists($items, $column) ? $items->{$column} : false;
  99. }else{
  100. $oldValue = false;
  101. }
  102. if (empty($value) && $value != 0) {
  103. $value = '';
  104. }
  105. if ($value === $oldValue) {
  106. return false;
  107. }
  108. if (false === $oldValue) {
  109. return $this->_addMeta($boardID, $column, $value); // 새로 추가
  110. }
  111. return $this->_updateMeta($boardID, $column, $value); // 수정
  112. }
  113. /**
  114. * 등록
  115. */
  116. private function _addMeta($boardID = 0, $column = '', $value = ''): mixed
  117. {
  118. if(!$boardID) {
  119. return false;
  120. }
  121. $column = trim($column);
  122. if (empty($column)) {
  123. return false;
  124. }
  125. $insertData = [
  126. $this->primaryKey => $boardID,
  127. $this->metaKey => $column,
  128. $this->metaValue => $value
  129. ];
  130. return $this->insert($insertData);
  131. }
  132. /**
  133. * 수정
  134. */
  135. private function _updateMeta($boardID = 0, $column = '', $value = ''): mixed
  136. {
  137. if(!$boardID) {
  138. return false;
  139. }
  140. $column = trim($column);
  141. if (empty($column)) {
  142. return false;
  143. }
  144. $updateData = [
  145. $this->metaValue => $value
  146. ];
  147. return $this->where($this->primaryKey, $boardID)->where($this->metaKey, $column)->update($updateData);
  148. }
  149. }