|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LRC\Repository; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Base class for database-backed soft-deletion-aware repositories for data access. |
|
7
|
|
|
*/ |
|
8
|
|
|
class SoftDbRepository extends DbRepository implements SoftRepositoryInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string Soft deletion attribute. |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $deleted; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Constructor. |
|
18
|
|
|
* |
|
19
|
|
|
* @param \Anax\Database\DatabaseQueryBuilder $db Database service. |
|
20
|
|
|
* @param string $table Database table name. |
|
21
|
|
|
* @param string $modelClass Model class name. |
|
22
|
|
|
* @param string $deleted Soft deletion attribute. |
|
23
|
|
|
* @param string $key Primary key column. |
|
24
|
|
|
*/ |
|
25
|
15 |
|
public function __construct($db, $table, $modelClass, $deleted = 'deleted', $key = 'id') |
|
26
|
|
|
{ |
|
27
|
15 |
|
parent::__construct($db, $table, $modelClass, $key); |
|
28
|
15 |
|
$this->deleted = $deleted; |
|
29
|
15 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Return the name of the attribute used to mark soft deletion. |
|
34
|
|
|
*/ |
|
35
|
3 |
|
public function getDeletedAttribute() |
|
36
|
|
|
{ |
|
37
|
3 |
|
return $this->deleted; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Find and return first entry by key, ignoring soft-deleted entries. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string|null $column Key column name (pass null to use registered primary key). |
|
45
|
|
|
* @param mixed $value Key value. |
|
46
|
|
|
* |
|
47
|
|
|
* @return mixed Model instance. |
|
48
|
|
|
*/ |
|
49
|
8 |
|
public function findSoft($column, $value) |
|
50
|
|
|
{ |
|
51
|
8 |
|
return $this->getFirstSoft((is_null($column) ? $this->key : $column) . ' = ?', [$value]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Retrieve first entry ignoring soft-deleted ones, optionally filtered by search criteria. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $conditions Where conditions. |
|
59
|
|
|
* @param array $values Array of condition values to bind. |
|
60
|
|
|
* @param array $options Query options. |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed Model instance. |
|
63
|
|
|
*/ |
|
64
|
9 |
|
public function getFirstSoft($conditions = null, $values = [], $options = []) |
|
65
|
|
|
{ |
|
66
|
9 |
|
return $this->processSingleResult($this->executeQuerySoft(null, $conditions, $values, $options)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Retrieve all entries ignoring soft-deleted ones, optionally filtered by search criteria. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $conditions Where conditions. |
|
74
|
|
|
* @param array $values Array of condition values to bind. |
|
75
|
|
|
* @param array $options Query options. |
|
76
|
|
|
* |
|
77
|
|
|
* @return array Array of all matching entries. |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function getAllSoft($conditions = null, $values = [], $options = []) |
|
80
|
|
|
{ |
|
81
|
2 |
|
return $this->processMultipleResults($this->executeQuerySoft(null, $conditions, $values, $options)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Soft delete entry. |
|
87
|
|
|
* |
|
88
|
|
|
* @param mixed $model Model instance. |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function deleteSoft($model) |
|
91
|
|
|
{ |
|
92
|
1 |
|
$model->deleted = date('Y-m-d H:i:s'); |
|
93
|
1 |
|
$this->save($model); |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Restore soft-deleted entry. |
|
99
|
|
|
* |
|
100
|
|
|
* @param mixed $model Model instance. |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function restoreSoft($model) |
|
103
|
|
|
{ |
|
104
|
1 |
|
$model->deleted = null; |
|
105
|
1 |
|
$this->save($model); |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Count entries ignoring soft-deleted ones, optionally filtered by search criteria. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $conditions Where conditions. |
|
113
|
|
|
* @param array $values Array of condition values to bind. |
|
114
|
|
|
* |
|
115
|
|
|
* @return int Number of entries. |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public function countSoft($conditions = null, $values = []) |
|
118
|
|
|
{ |
|
119
|
1 |
|
$res = $this->executeQuerySoft('COUNT(' . $this->key . ') AS num', $conditions, $values) |
|
120
|
1 |
|
->fetch(); |
|
121
|
1 |
|
return (isset($res->num) ? (int)$res->num : 0); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Execute soft-deletion-aware query for selection methods. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $select Selection criteria. |
|
129
|
|
|
* @param string $conditions Where conditions. |
|
130
|
|
|
* @param array $values Array of where condition values to bind. |
|
131
|
|
|
* @param array $options Query options. |
|
132
|
|
|
* |
|
133
|
|
|
* @return \Anax\Database\DatabaseQueryBuilder Database service instance with executed internal query. |
|
134
|
|
|
*/ |
|
135
|
11 |
|
protected function executeQuerySoft($select = null, $conditions = null, $values = [], $options = []) |
|
136
|
|
|
{ |
|
137
|
11 |
|
$delCond = $this->deleted . ' IS NULL'; |
|
138
|
11 |
|
$conditions = (is_null($conditions) ? $delCond : "($conditions) AND $delCond"); |
|
139
|
11 |
|
return $this->executeQuery($select, $conditions, $values, $options); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|