1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.7 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Model\Traits; |
16
|
|
|
|
17
|
|
|
use Quantum\Paginator\Exceptions\PaginatorException; |
18
|
|
|
use Quantum\Tests\_root\shared\Models\Products; |
19
|
|
|
use Quantum\App\Exceptions\BaseException; |
20
|
|
|
use Quantum\Model\ModelCollection; |
21
|
|
|
use Quantum\Paginator\Paginator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Trait SoftDeletes |
25
|
|
|
* @package Quantum\Model |
26
|
|
|
*/ |
27
|
|
|
trait SoftDeletes |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
protected $includeTrashed = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Soft delete the model by setting the deleted_at timestamp. |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
public function delete(): bool |
40
|
|
|
{ |
41
|
|
|
$this->prop($this->getDeleteAtColumn(), date('Y-m-d H:i:s')); |
|
|
|
|
42
|
|
|
return $this->save(); |
|
|
|
|
43
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Restore a soft deleted model. |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function restore(): bool |
51
|
|
|
{ |
52
|
|
|
$this->prop($this->getDeleteAtColumn(), null); |
53
|
|
|
return $this->save(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Force delete the model from the database. |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function forceDelete(): bool |
61
|
|
|
{ |
62
|
|
|
return parent::delete(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Include soft deleted records in the query. |
67
|
|
|
* @return static |
68
|
|
|
*/ |
69
|
|
|
public function withTrashed(): self |
70
|
|
|
{ |
71
|
|
|
$this->includeTrashed = true; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return only soft deleted records. |
78
|
|
|
* @return static |
79
|
|
|
*/ |
80
|
|
|
public function onlyTrashed(): self |
81
|
|
|
{ |
82
|
|
|
$this->includeTrashed = true; |
83
|
|
|
|
84
|
|
|
$this->ormInstance->isNotNull($this->getDeleteAtColumn()); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get all non-deleted records unless withTrashed is called. |
91
|
|
|
* @return ModelCollection |
92
|
|
|
*/ |
93
|
|
|
public function get(): ModelCollection |
94
|
|
|
{ |
95
|
|
|
$this->applySoftDeleteScope(); |
96
|
|
|
|
97
|
|
|
return parent::get(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Paginate non-deleted records unless withTrashed is called. |
102
|
|
|
* @param int $perPage |
103
|
|
|
* @param int $currentPage |
104
|
|
|
* @return Paginator |
105
|
|
|
* @throws BaseException |
106
|
|
|
* @throws PaginatorException |
107
|
|
|
*/ |
108
|
|
|
public function paginate(int $perPage, int $currentPage = 1): Paginator |
109
|
|
|
{ |
110
|
|
|
$this->applySoftDeleteScope(); |
111
|
|
|
|
112
|
|
|
return parent::paginate($perPage, $currentPage); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Count all non-deleted records unless withTrashed() is called. |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
|
|
public function count(): int |
120
|
|
|
{ |
121
|
|
|
$this->applySoftDeleteScope(); |
122
|
|
|
|
123
|
|
|
return parent::count(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Find one record by its ID, excluding soft deleted unless withTrashed() is called. |
128
|
|
|
* @param int $id |
129
|
|
|
* @return static |
130
|
|
|
*/ |
131
|
|
|
public function findOne(int $id): self |
132
|
|
|
{ |
133
|
|
|
$this->applySoftDeleteScope(); |
134
|
|
|
|
135
|
|
|
parent::findOne($id); |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Find one record by column and value, excluding soft deleted unless withTrashed() is called. |
142
|
|
|
* @param string $column |
143
|
|
|
* @param $value |
144
|
|
|
* @return static |
145
|
|
|
*/ |
146
|
|
|
public function findOneBy(string $column, $value): self |
147
|
|
|
{ |
148
|
|
|
$this->applySoftDeleteScope(); |
149
|
|
|
|
150
|
|
|
parent::findOneBy($column, $value); |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get the first record, excluding soft deleted unless withTrashed() is called. |
157
|
|
|
* @return static |
158
|
|
|
*/ |
159
|
|
|
public function first(): self |
160
|
|
|
{ |
161
|
|
|
$this->applySoftDeleteScope(); |
162
|
|
|
|
163
|
|
|
parent::first(); |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Apply soft delete scope to the current query if not including trashed. |
170
|
|
|
*/ |
171
|
|
|
protected function applySoftDeleteScope(): void |
172
|
|
|
{ |
173
|
|
|
if (!$this->includeTrashed) { |
174
|
|
|
$this->ormInstance->isNull($this->getDeleteAtColumn()); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the column name used for soft deletes. |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
protected function getDeleteAtColumn(): string |
183
|
|
|
{ |
184
|
|
|
if (defined(static::class . '::DELETED_AT')) { |
185
|
|
|
return static::DELETED_AT; |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return 'deleted_at'; |
189
|
|
|
} |
190
|
|
|
} |