CommentMeta.php 4.1 KB

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