SoftDelete.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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->db(false)->removeWhereField($field);
  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->db(false)->where($field, 'exp', 'is not null');
  40. }
  41. /**
  42. * 删除当前的记录
  43. * @access public
  44. * @param bool $force 是否强制删除
  45. * @return integer
  46. */
  47. public function delete($force = false)
  48. {
  49. if (false === $this->trigger('before_delete', $this)) {
  50. return false;
  51. }
  52. $name = $this->getDeleteTimeField();
  53. if (!$force) {
  54. // 软删除
  55. $this->change[] = $name;
  56. $this->data[$name] = $this->autoWriteTimestamp($name);
  57. $result = $this->isUpdate()->save();
  58. } else {
  59. $result = $this->db(false)->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. $where[$name] = ['not null', ''];
  107. }
  108. // 恢复删除
  109. return $this->db(false)->removeWhereField($this->getDeleteTimeField(true))->where($where)->update([$name => null]);
  110. }
  111. /**
  112. * 查询默认不包含软删除数据
  113. * @access protected
  114. * @param Query $query 查询对象
  115. * @return void
  116. */
  117. protected function base($query)
  118. {
  119. $field = $this->getDeleteTimeField(true);
  120. $query->where($field, 'null');
  121. }
  122. /**
  123. * 获取软删除字段
  124. * @access public
  125. * @param bool $read 是否查询操作 写操作的时候会自动去掉表别名
  126. * @return string
  127. */
  128. protected function getDeleteTimeField($read = false)
  129. {
  130. $field = isset($this->deleteTime) ? $this->deleteTime : 'delete_time';
  131. if (!strpos($field, '.')) {
  132. $field = $this->db(false)->getTable() . '.' . $field;
  133. }
  134. if (!$read && strpos($field, '.')) {
  135. $array = explode('.', $field);
  136. $field = array_pop($array);
  137. }
  138. return $field;
  139. }
  140. }