|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace miBadger\ActiveRecord\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use miBadger\Query\Query; |
|
6
|
|
|
use miBadger\ActiveRecord\ColumnProperty; |
|
7
|
|
|
|
|
8
|
|
|
const TRAIT_SOFT_DELETE_FIELD_KEY = "soft_delete"; |
|
9
|
|
|
|
|
10
|
|
|
trait SoftDelete |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var boolean the soft delete status for the entity this trait is embedded into. */ |
|
13
|
|
|
protected $softDelete; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* this method is required to be called in the constructor for each class that uses this trait. |
|
17
|
|
|
* It adds the required fields to the table definition and registers hooks |
|
18
|
|
|
*/ |
|
19
|
11 |
|
protected function initSoftDelete() |
|
20
|
|
|
{ |
|
21
|
11 |
|
$this->softDelete = false; |
|
22
|
|
|
|
|
23
|
11 |
|
$this->extendTableDefinition(TRAIT_SOFT_DELETE_FIELD_KEY, [ |
|
24
|
11 |
|
'value' => &$this->softDelete, |
|
25
|
|
|
'validate' => null, |
|
26
|
11 |
|
'default' => 0, |
|
27
|
11 |
|
'type' => 'INT', |
|
28
|
11 |
|
'length' => 1, |
|
29
|
11 |
|
'properties' => ColumnProperty::NOT_NULL |
|
30
|
|
|
]); |
|
31
|
|
|
|
|
32
|
11 |
|
$this->registerSearchHook(TRAIT_SOFT_DELETE_FIELD_KEY, 'softDeleteSearchHook'); |
|
33
|
11 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The hook that gets called whenever a query is made |
|
37
|
|
|
*/ |
|
38
|
1 |
|
protected function softDeleteSearchHook(Query $query) |
|
39
|
|
|
{ |
|
40
|
1 |
|
$query->where(TRAIT_SOFT_DELETE_FIELD_KEY, '=', 0); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* returns the name for the soft delete field in the database |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function getSoftDeleteFieldName() |
|
48
|
|
|
{ |
|
49
|
1 |
|
return TRAIT_SOFT_DELETE_FIELD_KEY; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Mark the current record as soft deleted |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function softDelete() |
|
57
|
|
|
{ |
|
58
|
2 |
|
$this->softDelete = true; |
|
59
|
2 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Undo the current soft deletion status (mark it as non-soft deleted) |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function softRestore() |
|
67
|
|
|
{ |
|
68
|
1 |
|
$this->softDelete = false; |
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* returns the current soft deletion status |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function getDeletionStatus() |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this->softDelete; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
abstract protected function extendTableDefinition($columnName, $definition); |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
abstract protected function registerSearchHook($columnName, $fn); |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
abstract protected function registerDeleteHook($columnName, $fn); |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
abstract protected function registerUpdateHook($columnName, $fn); |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return void |
|
103
|
|
|
*/ |
|
104
|
|
|
abstract protected function registerReadHook($columnName, $fn); |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
abstract protected function registerCreateHook($columnName, $fn); |
|
110
|
|
|
|
|
111
|
|
|
} |