SoftDelete.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // 删除条件
  60. $where = $this->getWhere();
  61. // 删除当前模型数据
  62. $result = $this->getQuery()->where($where)->delete();
  63. }
  64. // 关联删除
  65. if (!empty($this->relationWrite)) {
  66. foreach ($this->relationWrite as $key => $name) {
  67. $name = is_numeric($key) ? $name : $key;
  68. $model = $this->getAttr($name);
  69. if ($model instanceof Model) {
  70. $model->delete($force);
  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. // 包含软删除数据
  89. $query = self::withTrashed();
  90. if (is_array($data) && key($data) !== 0) {
  91. $query->where($data);
  92. $data = null;
  93. } elseif ($data instanceof \Closure) {
  94. call_user_func_array($data, [ & $query]);
  95. $data = null;
  96. } elseif (is_null($data)) {
  97. return 0;
  98. }
  99. $resultSet = $query->select($data);
  100. $count = 0;
  101. if ($resultSet) {
  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. $name = $this->getDeleteTimeField();
  118. if (empty($where)) {
  119. $pk = $this->getPk();
  120. $where[$pk] = $this->getData($pk);
  121. }
  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 void
  133. */
  134. protected function base($query)
  135. {
  136. $field = $this->getDeleteTimeField(true);
  137. $query->useSoftDelete($field);
  138. }
  139. /**
  140. * 获取软删除字段
  141. * @access public
  142. * @param bool $read 是否查询操作 写操作的时候会自动去掉表别名
  143. * @return string
  144. */
  145. protected function getDeleteTimeField($read = false)
  146. {
  147. $field = property_exists($this, 'deleteTime') && isset($this->deleteTime) ? $this->deleteTime : 'delete_time';
  148. if (!strpos($field, '.')) {
  149. $field = '__TABLE__.' . $field;
  150. }
  151. if (!$read && strpos($field, '.')) {
  152. $array = explode('.', $field);
  153. $field = array_pop($array);
  154. }
  155. return $field;
  156. }
  157. }