SoftDelete.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace traits\model;
  3. use think\db\Query;
  4. use think\Model;
  5. /**
  6. * @mixin \Think\Model
  7. */
  8. trait SoftDelete
  9. {
  10. /**
  11. * 判断当前实例是否被软删除
  12. * @access public
  13. * @return boolean
  14. */
  15. public function trashed()
  16. {
  17. $field = $this->getDeleteTimeField();
  18. if ($field && !empty($this->data[$field])) {
  19. return true;
  20. }
  21. return false;
  22. }
  23. /**
  24. * 查询包含软删除的数据
  25. * @access public
  26. * @return Query
  27. */
  28. public static function withTrashed()
  29. {
  30. return (new static )->getQuery();
  31. }
  32. /**
  33. * 只查询软删除数据
  34. * @access public
  35. * @return Query
  36. */
  37. public static function onlyTrashed()
  38. {
  39. $model = new static();
  40. $field = $model->getDeleteTimeField(true);
  41. if ($field) {
  42. return $model->getQuery()->useSoftDelete($field, ['not null', '']);
  43. } else {
  44. return $model->getQuery();
  45. }
  46. }
  47. /**
  48. * 删除当前的记录
  49. * @access public
  50. * @param bool $force 是否强制删除
  51. * @return integer
  52. */
  53. public function delete($force = false)
  54. {
  55. if (false === $this->trigger('before_delete', $this)) {
  56. return false;
  57. }
  58. $name = $this->getDeleteTimeField();
  59. if ($name && !$force) {
  60. // 软删除
  61. $this->data[$name] = $this->autoWriteTimestamp($name);
  62. $result = $this->isUpdate()->save();
  63. } else {
  64. // 强制删除当前模型数据
  65. $result = $this->getQuery()->where($this->getWhere())->delete();
  66. }
  67. // 关联删除
  68. if (!empty($this->relationWrite)) {
  69. foreach ($this->relationWrite as $key => $name) {
  70. $name = is_numeric($key) ? $name : $key;
  71. $result = $this->getRelation($name);
  72. if ($result instanceof Model) {
  73. $result->delete();
  74. } elseif ($result instanceof Collection || is_array($result)) {
  75. foreach ($result as $model) {
  76. $model->delete();
  77. }
  78. }
  79. }
  80. }
  81. $this->trigger('after_delete', $this);
  82. // 清空原始数据
  83. $this->origin = [];
  84. return $result;
  85. }
  86. /**
  87. * 删除记录
  88. * @access public
  89. * @param mixed $data 主键列表(支持闭包查询条件)
  90. * @param bool $force 是否强制删除
  91. * @return integer 成功删除的记录数
  92. */
  93. public static function destroy($data, $force = false)
  94. {
  95. if (is_null($data)) {
  96. return 0;
  97. }
  98. // 包含软删除数据
  99. $query = self::withTrashed();
  100. if (is_array($data) && key($data) !== 0) {
  101. $query->where($data);
  102. $data = null;
  103. } elseif ($data instanceof \Closure) {
  104. call_user_func_array($data, [ & $query]);
  105. $data = null;
  106. }
  107. $count = 0;
  108. if ($resultSet = $query->select($data)) {
  109. foreach ($resultSet as $data) {
  110. $result = $data->delete($force);
  111. $count += $result;
  112. }
  113. }
  114. return $count;
  115. }
  116. /**
  117. * 恢复被软删除的记录
  118. * @access public
  119. * @param array $where 更新条件
  120. * @return integer
  121. */
  122. public function restore($where = [])
  123. {
  124. if (empty($where)) {
  125. $pk = $this->getPk();
  126. $where[$pk] = $this->getData($pk);
  127. }
  128. $name = $this->getDeleteTimeField();
  129. if ($name) {
  130. // 恢复删除
  131. return $this->getQuery()
  132. ->useSoftDelete($name, ['not null', ''])
  133. ->where($where)
  134. ->update([$name => null]);
  135. } else {
  136. return 0;
  137. }
  138. }
  139. /**
  140. * 查询默认不包含软删除数据
  141. * @access protected
  142. * @param Query $query 查询对象
  143. * @return Query
  144. */
  145. protected function base($query)
  146. {
  147. $field = $this->getDeleteTimeField(true);
  148. return $field ? $query->useSoftDelete($field) : $query;
  149. }
  150. /**
  151. * 获取软删除字段
  152. * @access public
  153. * @param bool $read 是否查询操作(写操作的时候会自动去掉表别名)
  154. * @return string
  155. */
  156. protected function getDeleteTimeField($read = false)
  157. {
  158. $field = property_exists($this, 'deleteTime') && isset($this->deleteTime) ?
  159. $this->deleteTime :
  160. 'delete_time';
  161. if (false === $field) {
  162. return false;
  163. }
  164. if (!strpos($field, '.')) {
  165. $field = '__TABLE__.' . $field;
  166. }
  167. if (!$read && strpos($field, '.')) {
  168. $array = explode('.', $field);
  169. $field = array_pop($array);
  170. }
  171. return $field;
  172. }
  173. }