1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Starlit Db. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016 Starweb / Ehandelslogik i Lund AB |
6
|
|
|
* @license BSD 3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Starlit\Db; |
10
|
|
|
|
11
|
|
|
use Starlit\Db\Exception; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A database entity service is intended to handle database operations for existing |
15
|
|
|
* entity objects, i.e. load, save and load secondary objects etc. |
16
|
|
|
* |
17
|
|
|
* @author Andreas Nilsson <http://github.com/jandreasn> |
18
|
|
|
*/ |
19
|
|
|
class BasicDbEntityService |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The database adapter/connection/handler. |
23
|
|
|
* |
24
|
|
|
* @var Db |
25
|
|
|
*/ |
26
|
|
|
protected $db; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
* |
31
|
|
|
* @param Db $db |
32
|
|
|
*/ |
33
|
15 |
|
public function __construct(Db $db) |
34
|
|
|
{ |
35
|
15 |
|
$this->db = $db; |
36
|
15 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
*/ |
40
|
1 |
|
public function reconnect() |
41
|
|
|
{ |
42
|
1 |
|
$this->db->disconnect(); |
43
|
1 |
|
$this->db->connect(); |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Load object's values from database table. |
48
|
|
|
* |
49
|
|
|
* @param AbstractDbEntity $dbEntity |
50
|
|
|
* @throws Exception\EntityNotFoundException |
51
|
|
|
*/ |
52
|
3 |
|
public function load(AbstractDbEntity $dbEntity) |
53
|
|
|
{ |
54
|
|
|
// Check that the primary key is set |
55
|
3 |
|
if (!$dbEntity->getPrimaryDbValue()) { |
56
|
1 |
|
throw new \InvalidArgumentException( |
57
|
|
|
'Database entity can not be loaded because primary value is not set' |
58
|
1 |
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Fetch ad from db |
62
|
2 |
|
$row = $this->db->fetchRow( |
63
|
|
|
' |
64
|
|
|
SELECT * |
65
|
2 |
|
FROM `' . $dbEntity->getDbTableName() . '` |
66
|
2 |
|
WHERE ' . $this->getPrimaryKeyWhereSql($dbEntity) . ' |
67
|
2 |
|
', |
68
|
2 |
|
$this->getPrimaryKeyWhereParameters($dbEntity) |
|
|
|
|
69
|
2 |
|
); |
70
|
|
|
|
71
|
2 |
|
if (!$row) { |
72
|
1 |
|
throw new Exception\EntityNotFoundException("Db entity[{$dbEntity->getPrimaryDbValue()}] does not exist"); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$dbEntity->setDbDataFromRow($row); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Save entity to database table. |
80
|
|
|
* |
81
|
|
|
* @param AbstractDbEntity $dbEntity |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
9 |
|
public function save(AbstractDbEntity $dbEntity) |
85
|
|
|
{ |
86
|
9 |
|
if ($dbEntity->shouldBeDeletedFromDbOnSave()) { |
87
|
|
|
// Only delete if previously saved to db |
88
|
2 |
|
if ($dbEntity->getPrimaryDbValue()) { |
89
|
1 |
|
$this->delete($dbEntity); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
2 |
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
7 |
|
if ($dbEntity->shouldInsertOnDbSave()) { |
96
|
|
|
// Note that database data always contains all properties, with defaults for non set properties |
97
|
4 |
|
$dataToSave = $dbEntity->getDbData(); |
98
|
4 |
|
} else { |
99
|
3 |
|
if ($dbEntity->hasModifiedDbProperties()) { |
100
|
2 |
|
$dataToSave = $dbEntity->getModifiedDbData(); |
101
|
2 |
|
} else { |
102
|
|
|
// Return if no value has been modified and it's not an insert |
103
|
|
|
// (we always want to insert if no id exist, since some child objects might |
104
|
|
|
// depend on the this primary id being available) |
105
|
1 |
|
return false; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// We don't the want to insert/update primary value unless forced insert |
110
|
6 |
|
if (!$dbEntity->shouldForceDbInsertOnSave()) { |
111
|
6 |
|
$primaryKey = $dbEntity->getPrimaryDbPropertyKey(); |
112
|
6 |
|
if (is_array($primaryKey)) { |
113
|
1 |
|
foreach ($primaryKey as $keyPart) { |
114
|
1 |
|
unset($dataToSave[$keyPart]); |
115
|
1 |
|
} |
116
|
1 |
|
} else { |
117
|
5 |
|
unset($dataToSave[$primaryKey]); |
118
|
|
|
} |
119
|
6 |
|
} |
120
|
|
|
|
121
|
|
|
// Convert & check data |
122
|
6 |
|
$sqlData = []; |
123
|
6 |
|
foreach ($dataToSave as $propertyName => $value) { |
124
|
|
|
if ($value |
125
|
6 |
|
&& $dbEntity->getDbPropertyMaxLength($propertyName) |
126
|
6 |
|
&& mb_strlen($value) > $dbEntity->getDbPropertyMaxLength($propertyName) |
127
|
6 |
|
) { |
128
|
1 |
|
throw new \RuntimeException( |
129
|
1 |
|
"Database field \"{$propertyName}\" exceeds field max length (value: \"{$value}\")" |
130
|
1 |
|
); |
131
|
5 |
|
} elseif (empty($value) && $dbEntity->getDbPropertyNonEmpty($propertyName)) { |
132
|
1 |
|
throw new \RuntimeException("Database field \"{$propertyName}\" is empty and required"); |
133
|
5 |
|
} elseif ((((string) $value) === '') && $dbEntity->getDbPropertyRequired($propertyName)) { |
134
|
1 |
|
throw new \RuntimeException("Database field \"{$propertyName}\" is required to be set"); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Set data keys db field format |
138
|
4 |
|
$fieldName = $dbEntity->getDbFieldName($propertyName); |
139
|
4 |
|
$sqlData[$fieldName] = $value; |
140
|
4 |
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
// Insert new database row |
144
|
3 |
|
if ($dbEntity->shouldInsertOnDbSave()) { |
145
|
1 |
|
$this->db->insert( |
146
|
1 |
|
$dbEntity->getDbTableName(), |
147
|
|
|
$sqlData |
148
|
1 |
|
); |
149
|
|
|
|
150
|
1 |
|
if (!is_array($dbEntity->getPrimaryDbPropertyKey()) |
151
|
1 |
|
&& !empty($lastInsertId = $this->db->getLastInsertId()) |
152
|
1 |
|
) { |
153
|
|
|
$dbEntity->setPrimaryDbValue($lastInsertId); |
154
|
|
|
} |
155
|
|
|
// Update existing database row |
156
|
1 |
|
} else { |
157
|
2 |
|
$this->db->update( |
158
|
2 |
|
$dbEntity->getDbTableName(), |
159
|
2 |
|
$sqlData, |
160
|
2 |
|
$this->getPrimaryKeyWhereSql($dbEntity), |
161
|
2 |
|
$this->getPrimaryKeyWhereParameters($dbEntity) |
|
|
|
|
162
|
|
|
|
163
|
2 |
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
3 |
|
$dbEntity->clearModifiedDbProperties(); |
167
|
3 |
|
$dbEntity->setForceDbInsertOnSave(false); |
168
|
|
|
|
169
|
3 |
|
return true; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Delete entity's corresponding database row. |
174
|
|
|
* |
175
|
|
|
* @param AbstractDbEntity $dbEntity |
176
|
|
|
*/ |
177
|
3 |
|
public function delete(AbstractDbEntity $dbEntity) |
178
|
|
|
{ |
179
|
3 |
|
if (!$dbEntity->getPrimaryDbValue()) { |
180
|
1 |
|
throw new \InvalidArgumentException('Primary database value not set'); |
181
|
|
|
} |
182
|
|
|
|
183
|
2 |
|
$this->db->exec( |
184
|
2 |
|
'DELETE FROM `' . $dbEntity->getDbTableName() . '` WHERE ' . $this->getPrimaryKeyWhereSql($dbEntity), |
185
|
2 |
|
$this->getPrimaryKeyWhereParameters($dbEntity) |
|
|
|
|
186
|
2 |
|
); |
187
|
|
|
|
188
|
2 |
|
$dbEntity->setDeleted(); |
189
|
2 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param AbstractDbEntity $dbEntity |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
6 |
|
private function getPrimaryKeyWhereSql(AbstractDbEntity $dbEntity) |
196
|
|
|
{ |
197
|
6 |
|
if (is_array($dbEntity->getPrimaryDbPropertyKey())) { |
198
|
1 |
|
$whereStrings = []; |
199
|
1 |
|
foreach ($dbEntity->getPrimaryDbPropertyKey() as $primaryKeyPart) { |
200
|
1 |
|
$whereStrings[] = '`' . $primaryKeyPart . '` = ?'; |
201
|
1 |
|
} |
202
|
1 |
|
$whereSql = implode(' AND ', $whereStrings); |
203
|
1 |
|
} else { |
204
|
5 |
|
$whereSql = '`' . $dbEntity->getPrimaryDbFieldKey() . '` = ?'; |
205
|
|
|
} |
206
|
|
|
|
207
|
6 |
|
return $whereSql; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param AbstractDbEntity $dbEntity |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
6 |
|
private function getPrimaryKeyWhereParameters(AbstractDbEntity $dbEntity) |
215
|
|
|
{ |
216
|
6 |
|
if (is_array($dbEntity->getPrimaryDbPropertyKey())) { |
217
|
1 |
|
$whereParameters = $dbEntity->getPrimaryDbValue(); |
218
|
1 |
|
} else { |
219
|
5 |
|
$whereParameters = [$dbEntity->getPrimaryDbValue()]; |
220
|
|
|
} |
221
|
|
|
|
222
|
6 |
|
return $whereParameters; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: