Repository navigation
Expand file tree
/
Copy pathSoftDeletes.php
More file actions
170 lines (140 loc) · 4.54 KB
/
Copy pathSoftDeletes.php
File metadata and controls
170 lines (140 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model;
use Psr\EventDispatcher\StoppableEventInterface;
use function Hyperf\Tappable\tap;
/**
* @method static \Hyperf\Database\Model\Builder<static> withTrashed(bool $withTrashed = true)
* @method static \Hyperf\Database\Model\Builder<static> onlyTrashed()
* @method static \Hyperf\Database\Model\Builder<static> withoutTrashed()
* @method static static restoreOrCreate(array<string, mixed> $attributes = [], array<string, mixed> $values = [])
* @method static static createOrRestore(array<string, mixed> $attributes = [], array<string, mixed> $values = [])
*/
trait SoftDeletes
{
/**
* Indicates if the model is currently force deleting.
*/
protected bool $forceDeleting = false;
/**
* Boot the soft deleting trait for a model.
*/
public static function bootSoftDeletes()
{
static::addGlobalScope(new SoftDeletingScope());
}
/**
* Force a hard delete on a soft deleted model.
*
* @return null|bool
*/
public function forceDelete()
{
if ($event = $this->fireModelEvent('forceDeleting')) {
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
return false;
}
}
$this->forceDeleting = true;
return tap($this->delete(), function ($deleted) {
$this->forceDeleting = false;
if ($deleted) {
$this->fireModelEvent('forceDeleted');
}
});
}
/**
* Restore a soft-deleted model instance.
*
* @return null|bool
*/
public function restore()
{
// If the restoring event does not return false, we will proceed with this
// restore operation. Otherwise, we bail out so the developer will stop
// the restore totally. We will clear the deleted timestamp and save.
if ($event = $this->fireModelEvent('restoring')) {
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
return false;
}
}
$this->{$this->getDeletedAtColumn()} = null;
// Once we have saved the model, we will fire the "restored" event so this
// developer will do anything they need to after a restore operation is
// totally finished. Then we will return the result of the save call.
$this->exists = true;
$result = $this->save();
$this->fireModelEvent('restored');
return $result;
}
/**
* Determine if the model instance has been soft-deleted.
*
* @return bool
*/
public function trashed()
{
return ! is_null($this->{$this->getDeletedAtColumn()});
}
/**
* Determine if the model is currently force deleting.
*
* @return bool
*/
public function isForceDeleting()
{
return $this->forceDeleting;
}
/**
* Get the name of the "deleted at" column.
*
* @return string
*/
public function getDeletedAtColumn()
{
return defined('static::DELETED_AT') ? static::DELETED_AT : 'deleted_at';
}
/**
* Get the fully qualified "deleted at" column.
*
* @return string
*/
public function getQualifiedDeletedAtColumn()
{
return $this->qualifyColumn($this->getDeletedAtColumn());
}
/**
* Perform the actual delete query on this model instance.
*/
protected function performDeleteOnModel()
{
if ($this->forceDeleting) {
$this->exists = false;
return $this->newModelQuery()->where($this->getKeyName(), $this->getKey())->forceDelete();
}
return $this->runSoftDelete();
}
/**
* Perform the actual delete query on this model instance.
*/
protected function runSoftDelete()
{
$query = $this->newModelQuery()->where($this->getKeyName(), $this->getKey());
$time = $this->freshTimestamp();
$columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
$this->{$this->getDeletedAtColumn()} = $time;
if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) {
$this->{$this->getUpdatedAtColumn()} = $time;
$columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
}
$query->update($columns);
}
}