SoftDelete.php 4.5 KB

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