SoftDelete.php 4.9 KB

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