SoftDelete.php 3.9 KB

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