SoftDeleteTrait.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace addons\OperationCenter\common\models\traits;
  3. /**
  4. * 软删除工具类
  5. * Trait SoftDeleteTrait
  6. * @package app\common\trait_class
  7. */
  8. trait SoftDeleteTrait
  9. {
  10. /**
  11. * @var int 删除默认值
  12. */
  13. static $deleteDefault = 0;
  14. /**
  15. * 软删除
  16. * @return mixed
  17. * @throws \Exception
  18. */
  19. public function softDelete()
  20. {
  21. $this->{self::getDeletedAtAttribute()} = time();
  22. $ret = $this->save(false, [self::getDeletedAtAttribute()]);
  23. $this->afterSoftDelete();
  24. return $ret;
  25. }
  26. public function afterSoftDelete()
  27. {
  28. // Default implementation
  29. }
  30. /**
  31. * 软删除
  32. * @return mixed
  33. * @throws \Exception
  34. */
  35. public function delete()
  36. {
  37. return $this->softDelete();
  38. }
  39. /**
  40. * Gets the deleted_at attribute name
  41. *
  42. * @throws \Exception
  43. */
  44. static public function getDeletedAtAttribute()
  45. {
  46. return 'deleted_at';
  47. }
  48. /**
  49. * @return mixed
  50. * @throws \Exception
  51. */
  52. public function restore()
  53. {
  54. $this->{self::getDeletedAtAttribute()} = self::$deleteDefault;
  55. return $this->save(false, [self::getDeletedAtAttribute()]);
  56. }
  57. /**
  58. * 批量删除
  59. * @param null $condition
  60. * @param array $params
  61. * @return mixed
  62. * @throws \Exception
  63. */
  64. public static function deleteAll($condition = null, $params = [])
  65. {
  66. $deleteField = self::getDeletedAtAttribute();
  67. $deleteFieldValue["{$deleteField}"] = time();
  68. $command = static::getDb()->createCommand();
  69. $command->update(static::tableName(), $deleteFieldValue, $condition, $params);
  70. return $command->execute();
  71. }
  72. /**
  73. * 过滤软删除的数据
  74. * @return \yii\db\ActiveQuery
  75. * @throws \Exception
  76. */
  77. public static function find()
  78. {
  79. $deleteField = self::getDeletedAtAttribute();
  80. $where = [
  81. static::tableName() . '.' . "{$deleteField}" => self::$deleteDefault
  82. ];
  83. return parent::find()->andWhere($where);
  84. }
  85. /**
  86. * 查询包含软删除的数据
  87. * @return \yii\db\ActiveQuery
  88. * @throws \Exception
  89. */
  90. public static function withTrashedFind()
  91. {
  92. return parent::find();
  93. }
  94. /**
  95. * 只查询软删除数据
  96. * @return \yii\db\ActiveQuery
  97. * @throws \Exception
  98. */
  99. public static function onlyTrashedFind()
  100. {
  101. $deleteField = self::getDeletedAtAttribute();
  102. $where = ['not', [$deleteField => self::$deleteDefault]];
  103. return parent::find()->andWhere($where);
  104. }
  105. /**
  106. * 判断当前实例是否被软删除
  107. * @return false
  108. * @throws \Exception
  109. */
  110. public function trashed()
  111. {
  112. $field = $this->getDeletedAtAttribute();
  113. $softDelete = $this->$field ?? false;
  114. if ($field && ($softDelete === self::$deleteDefault)) {
  115. return false;
  116. }
  117. return true;
  118. }
  119. }