1
|
|
|
<?php |
2
|
|
|
namespace LmcUser\Mapper; |
3
|
|
|
|
4
|
|
|
use Laminas\Db\Adapter\Adapter; |
5
|
|
|
use Laminas\Db\Adapter\Driver\ResultInterface; |
6
|
|
|
use Laminas\Db\ResultSet\HydratingResultSet; |
7
|
|
|
use Laminas\Db\Sql\Select; |
8
|
|
|
use Laminas\Db\Sql\Sql; |
9
|
|
|
use Laminas\Db\Sql\TableIdentifier; |
10
|
|
|
use Laminas\Hydrator\ClassMethodsHydrator; |
11
|
|
|
use Laminas\Hydrator\HydratorInterface; |
12
|
|
|
use LmcUser\Db\Adapter\MasterSlaveAdapterInterface; |
13
|
|
|
use LmcUser\Entity\UserInterface as UserEntityInterface; |
14
|
|
|
use LmcUser\EventManager\EventProvider; |
15
|
|
|
|
16
|
|
|
abstract class AbstractDbMapper extends EventProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Adapter |
20
|
|
|
*/ |
21
|
|
|
protected $dbAdapter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Adapter |
25
|
|
|
*/ |
26
|
|
|
protected $dbSlaveAdapter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var HydratorInterface |
30
|
|
|
*/ |
31
|
|
|
protected $hydrator; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var UserEntityInterface |
35
|
|
|
*/ |
36
|
|
|
protected $entityPrototype; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var HydratingResultSet |
40
|
|
|
*/ |
41
|
|
|
protected $resultSetPrototype; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Select |
45
|
|
|
*/ |
46
|
|
|
protected $selectPrototype; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Sql |
50
|
|
|
*/ |
51
|
|
|
private $sql; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Sql |
55
|
|
|
*/ |
56
|
|
|
private $slaveSql; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $tableName; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var boolean |
65
|
|
|
*/ |
66
|
|
|
private $isInitialized = false; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Performs some basic initialization setup and checks before running a query |
70
|
|
|
* |
71
|
|
|
* @throws \Exception |
72
|
|
|
* @return null |
73
|
|
|
*/ |
74
|
|
|
protected function initialize() |
75
|
|
|
{ |
76
|
|
|
if ($this->isInitialized) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
if (!$this->dbAdapter instanceof Adapter) { |
80
|
|
|
throw new \Exception('No db adapter present'); |
81
|
|
|
} |
82
|
|
|
if (!$this->hydrator instanceof HydratorInterface) { |
83
|
|
|
$this->hydrator = new ClassMethodsHydrator(); |
84
|
|
|
} |
85
|
|
|
if (!is_object($this->entityPrototype)) { |
86
|
|
|
throw new \Exception('No entity prototype set'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->isInitialized = true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string|null $table |
94
|
|
|
* @return Select |
95
|
|
|
*/ |
96
|
|
|
protected function getSelect($table = null) |
97
|
|
|
{ |
98
|
|
|
$this->initialize(); |
99
|
|
|
|
100
|
|
|
return $this->getSlaveSql()->select($table ?: $this->getTableName()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param Select $select |
105
|
|
|
* @param UserEntityInterface|null $entityPrototype |
106
|
|
|
* @param HydratorInterface|null $hydrator |
107
|
|
|
* @return HydratingResultSet |
108
|
|
|
*/ |
109
|
|
|
protected function select(Select $select, UserEntityInterface $entityPrototype = null, HydratorInterface $hydrator = null) |
110
|
|
|
{ |
111
|
|
|
$this->initialize(); |
112
|
|
|
$stmt = $this->getSlaveSql()->prepareStatementForSqlObject($select); |
113
|
|
|
$resultSet = new HydratingResultSet( |
114
|
|
|
$hydrator ?: $this->getHydrator(), |
115
|
|
|
$entityPrototype ?: $this->getEntityPrototype() |
116
|
|
|
); |
117
|
|
|
$resultSet->initialize($stmt->execute()); |
118
|
|
|
return $resultSet; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param UserEntityInterface $entity |
123
|
|
|
* @param string|TableIdentifier|null $tableName |
124
|
|
|
* @param HydratorInterface|null $hydrator |
125
|
|
|
* @return ResultInterface |
126
|
|
|
*/ |
127
|
|
|
protected function insert(UserEntityInterface $entity, $tableName = null, HydratorInterface $hydrator = null) |
128
|
|
|
{ |
129
|
|
|
$this->initialize(); |
130
|
|
|
$tableName = $tableName ?: $this->tableName; |
131
|
|
|
$sql = $this->getSql()->setTable($tableName); |
132
|
|
|
$insert = $sql->insert(); |
133
|
|
|
$rowData = $this->entityToArray($entity, $hydrator); |
134
|
|
|
$insert->values($rowData); |
135
|
|
|
$statement = $sql->prepareStatementForSqlObject($insert); |
136
|
|
|
return $statement->execute(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param UserEntityInterface $entity |
141
|
|
|
* @param string|array|\Closure $where |
142
|
|
|
* @param string|TableIdentifier|null $tableName |
143
|
|
|
* @param HydratorInterface|null $hydrator |
144
|
|
|
* @return ResultInterface |
145
|
|
|
*/ |
146
|
|
|
protected function update(UserEntityInterface $entity, $where, $tableName = null, HydratorInterface $hydrator = null) |
147
|
|
|
{ |
148
|
|
|
$this->initialize(); |
149
|
|
|
$tableName = $tableName ?: $this->tableName; |
150
|
|
|
$sql = $this->getSql()->setTable($tableName); |
151
|
|
|
$update = $sql->update(); |
152
|
|
|
$rowData = $this->entityToArray($entity, $hydrator); |
153
|
|
|
$update->set($rowData) |
154
|
|
|
->where($where); |
155
|
|
|
$statement = $sql->prepareStatementForSqlObject($update); |
156
|
|
|
return $statement->execute(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string|array|\Closure $where |
161
|
|
|
* @param string|TableIdentifier|null $tableName |
162
|
|
|
* @return ResultInterface |
163
|
|
|
*/ |
164
|
|
|
protected function delete($where, $tableName = null) |
165
|
|
|
{ |
166
|
|
|
$tableName = $tableName ?: $this->tableName; |
167
|
|
|
$sql = $this->getSql()->setTable($tableName); |
168
|
|
|
$delete = $sql->delete(); |
169
|
|
|
$delete->where($where); |
170
|
|
|
$statement = $sql->prepareStatementForSqlObject($delete); |
171
|
|
|
return $statement->execute(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
protected function getTableName() |
178
|
|
|
{ |
179
|
|
|
return $this->tableName; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return UserEntityInterface |
184
|
|
|
*/ |
185
|
|
|
public function getEntityPrototype() |
186
|
|
|
{ |
187
|
|
|
return $this->entityPrototype; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param UserEntityInterface $entityPrototype |
192
|
|
|
* @return AbstractDbMapper |
193
|
|
|
*/ |
194
|
|
|
public function setEntityPrototype(UserEntityInterface $entityPrototype) |
195
|
|
|
{ |
196
|
|
|
$this->entityPrototype = $entityPrototype; |
197
|
|
|
$this->resultSetPrototype = null; |
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return Adapter |
203
|
|
|
*/ |
204
|
|
|
public function getDbAdapter() |
205
|
|
|
{ |
206
|
|
|
return $this->dbAdapter; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param Adapter $dbAdapter |
211
|
|
|
* @return AbstractDbMapper |
212
|
|
|
*/ |
213
|
|
|
public function setDbAdapter(Adapter $dbAdapter) |
214
|
|
|
{ |
215
|
|
|
$this->dbAdapter = $dbAdapter; |
216
|
|
|
if ($dbAdapter instanceof MasterSlaveAdapterInterface) { |
217
|
|
|
$this->setDbSlaveAdapter($dbAdapter->getSlaveAdapter()); |
218
|
|
|
} |
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return Adapter |
224
|
|
|
*/ |
225
|
|
|
public function getDbSlaveAdapter() |
226
|
|
|
{ |
227
|
|
|
return $this->dbSlaveAdapter ?: $this->dbAdapter; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param Adapter $dbSlaveAdapter |
232
|
|
|
* @return AbstractDbMapper |
233
|
|
|
*/ |
234
|
|
|
public function setDbSlaveAdapter(Adapter $dbSlaveAdapter) |
235
|
|
|
{ |
236
|
|
|
$this->dbSlaveAdapter = $dbSlaveAdapter; |
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return HydratorInterface |
242
|
|
|
*/ |
243
|
|
|
public function getHydrator() |
244
|
|
|
{ |
245
|
|
|
if (!$this->hydrator) { |
246
|
|
|
$this->hydrator = new ClassMethodsHydrator(false); |
247
|
|
|
} |
248
|
|
|
return $this->hydrator; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param HydratorInterface $hydrator |
253
|
|
|
* @return AbstractDbMapper |
254
|
|
|
*/ |
255
|
|
|
public function setHydrator(HydratorInterface $hydrator) |
256
|
|
|
{ |
257
|
|
|
$this->hydrator = $hydrator; |
258
|
|
|
$this->resultSetPrototype = null; |
259
|
|
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return Sql |
264
|
|
|
*/ |
265
|
|
|
protected function getSql() |
266
|
|
|
{ |
267
|
|
|
if (!$this->sql instanceof Sql) { |
268
|
|
|
$this->sql = new Sql($this->getDbAdapter()); |
269
|
|
|
} |
270
|
|
|
return $this->sql; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param Sql $sql |
275
|
|
|
* @return AbstractDbMapper |
276
|
|
|
*/ |
277
|
|
|
protected function setSql(Sql $sql) |
278
|
|
|
{ |
279
|
|
|
$this->sql = $sql; |
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return Sql |
285
|
|
|
*/ |
286
|
|
|
protected function getSlaveSql() |
287
|
|
|
{ |
288
|
|
|
if (!$this->slaveSql instanceof Sql) { |
289
|
|
|
$this->slaveSql = new Sql($this->getDbSlaveAdapter()); |
290
|
|
|
} |
291
|
|
|
return $this->slaveSql; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param Sql $sql |
296
|
|
|
* @return AbstractDbMapper |
297
|
|
|
*/ |
298
|
|
|
protected function setSlaveSql(Sql $sql) |
299
|
|
|
{ |
300
|
|
|
$this->slaveSql = $sql; |
301
|
|
|
return $this; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Uses the hydrator to convert the entity to an array. |
306
|
|
|
* |
307
|
|
|
* Use this method to ensure that you're working with an array. |
308
|
|
|
* |
309
|
|
|
* @param UserEntityInterface $entity |
310
|
|
|
* @param HydratorInterface|null $hydrator |
311
|
|
|
* @return array |
312
|
|
|
*/ |
313
|
|
|
protected function entityToArray(UserEntityInterface $entity, HydratorInterface $hydrator = null) |
314
|
|
|
{ |
315
|
|
|
if (!$hydrator) { |
316
|
|
|
$hydrator = $this->getHydrator(); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
return $hydrator->extract($entity); |
|
|
|
|
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
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: