You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

201 lines
4.9 KiB

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