SoftDelete.php 4.5 KB

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