ModelTrait.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace huosdk\h5ios\core\model;
  3. use think\Cache;
  4. use think\Loader;
  5. trait ModelTrait {
  6. public function getFieldType() {
  7. return $this->type;
  8. }
  9. public function __call($method, $args) {
  10. if (true == $this->isGetFunc($method)) {
  11. /* 获取字段 */
  12. $_id = $args[0];
  13. if (empty($_id)) {
  14. return null;
  15. }
  16. $_name = substr(strstr($method, 'ById', true), 3);
  17. $_field = Loader::parseName($_name);
  18. $this->data = $this->getInfoById($_id);
  19. return $this->getAttr($_field);
  20. } elseif (true == $this->isUpdateFunc($method)) {
  21. /* 更新字段 */
  22. $_id = $args[1];
  23. if (empty($_id)) {
  24. return false;
  25. }
  26. $_name = substr(strstr($method, 'ById', true), 6);
  27. $_field = Loader::parseName($_name);
  28. $_data = $this->getInfoById($_id);
  29. if (empty($_data)) {
  30. return false;
  31. }
  32. $_update_data[$_field] = $args[0];
  33. return $this->updateData($_update_data, $_id);
  34. }
  35. return parent::__call($method, $args); // TODO: Change the autogenerated stub
  36. }
  37. private function isGetFunc($method) {
  38. $_rs = substr_compare($method, 'ById', -strlen('ById'));
  39. $_start_str = substr($method, 0, strlen('get'));
  40. if (0 === $_rs && 'get' == $_start_str) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. private function isUpdateFunc($method) {
  46. $_rs = substr_compare($method, 'ById', -strlen('ById'));
  47. $_start_str = substr($method, 0, strlen('update'));
  48. if (0 === $_rs && 'update' == $_start_str) {
  49. return true;
  50. }
  51. return false;
  52. }
  53. /**
  54. * 获取单条记录缓存key
  55. *
  56. * @param int $id ID
  57. *
  58. * @return string
  59. */
  60. protected function getSingleCacheKey($id) {
  61. return $this->cache_key_prefix.$id;
  62. }
  63. /**
  64. * 添加数据
  65. *
  66. * @param array $data 需要添加的数据
  67. *
  68. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  69. */
  70. public function addData($data) {
  71. $_data = $data;
  72. $_id = parent::addData($_data);
  73. if (false === $_id) {
  74. return false;
  75. }
  76. /* TAG缓存操作 */
  77. return $_id;
  78. }
  79. /**
  80. * 通过ID获取信息
  81. *
  82. * @param int $id 主键ID
  83. *
  84. * @return array|false
  85. */
  86. public function getInfoById($id) {
  87. if ($this->useCache()) {
  88. /* 缓存操作 */
  89. $_single_cache_key = $this->getSingleCacheKey($id);
  90. Cache::rm($_single_cache_key);
  91. $_data = Cache::get($_single_cache_key);
  92. if (!empty($_data)) {
  93. return $_data;
  94. }
  95. }
  96. $_data = parent::getInfoById($id);
  97. if (empty($_data)) {
  98. return [];
  99. }
  100. if ($this->useCache()) {
  101. Cache::set($_single_cache_key, $_data);
  102. }
  103. return $_data;
  104. }
  105. /**
  106. * 更新单条数据
  107. *
  108. * @param array $data 数据
  109. * @param int $id ID
  110. *
  111. * @return bool
  112. */
  113. public function updateData($data, $id) {
  114. $_data = $data;
  115. $_rs = parent::updateData($_data, $id);
  116. if (false === $_rs) {
  117. return false;
  118. }
  119. if ($this->useCache()) {
  120. /* 缓存操作 */
  121. $_single_cache_key = $this->getSingleCacheKey($id);
  122. Cache::rm($_single_cache_key);
  123. /* TAG缓存操作 */
  124. }
  125. return true;
  126. }
  127. /**
  128. * 删除单条数据
  129. *
  130. * @param int $id ID
  131. * @param bool $is_complete 是否完成删除
  132. *
  133. * @return bool
  134. */
  135. public function deleteData($id, $is_complete = false) {
  136. $_rs = parent::deleteData($id, $is_complete);
  137. if (false == $_rs) {
  138. return false;
  139. }
  140. if ($this->useCache()) {
  141. /* 缓存操作 */
  142. $_single_cache_key = $this->getSingleCacheKey($id);
  143. Cache::rm($_single_cache_key);
  144. /* TAG缓存操作 */
  145. }
  146. return $_rs;
  147. }
  148. private function useCache() {
  149. if (property_exists($this, 'cache_key_prefix') && !empty($this->cache_key_prefix)) {
  150. return true;
  151. }
  152. return false;
  153. }
  154. }