1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleORM; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Description of AbstractDataMapper |
7
|
|
|
* |
8
|
|
|
* @author Dmitriy |
9
|
|
|
*/ |
10
|
|
|
abstract class AbstractDataMapper implements RepositoryInterface, MapperInterface |
11
|
|
|
{ |
12
|
|
|
protected $db; |
13
|
|
|
|
14
|
|
|
protected $entityTable; |
15
|
|
|
|
16
|
|
|
protected $key; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Использование join при выборке |
21
|
|
|
* @var type |
22
|
|
|
*/ |
23
|
|
|
protected $use_joins = false; |
24
|
|
|
|
25
|
|
|
protected $use_delete = false; |
26
|
|
|
|
27
|
|
|
public function __construct( QueryBuilderInterface $adapter, $db_name = null) {// \CI_DB_mysqli_driver //DatabaseAdapterInterface |
28
|
|
|
$this->db = $adapter; |
29
|
|
|
|
30
|
|
|
$this->entityTable = !empty($db_name)? "$db_name.".$this->setEntityTable() : $this->setEntityTable(); |
31
|
|
|
|
32
|
|
|
//$this->key = $this->getKey(); |
|
|
|
|
33
|
|
|
|
34
|
|
|
if($this->getEntityTable()=='' || $this->getPrimaryKey()==''){ |
35
|
|
|
throw new InvalidEntityPropertyException('Свойства entityTable или key не заданы'); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
|
41
|
|
|
function getEntityTable() { |
|
|
|
|
42
|
|
|
return $this->entityTable; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getAdapter() { |
46
|
|
|
return $this->db; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function findById($id) |
50
|
|
|
{ |
51
|
|
|
$Criteria = (new Specification())->setWhere($this->key , $id); |
52
|
|
|
|
53
|
|
|
return $this->findBySpecification($Criteria); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Cохранение сущности |
58
|
|
|
* @param \Core\Infrastructure\EntityInterface $Entity |
59
|
|
|
*/ |
60
|
|
|
public function save(EntityInterface $Entity) |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
$data = $this->unbuildEntity($Entity); |
64
|
|
|
|
65
|
|
|
$id = $data[$this->getPrimaryKey()]; |
66
|
|
|
unset($data[$this->getPrimaryKey()]); |
67
|
|
|
|
68
|
|
|
//insert |
69
|
|
|
if (empty($id)) { |
70
|
|
|
|
71
|
|
|
unset($data[$this->setSoftDeleteKey()]); |
72
|
|
|
|
73
|
|
|
$this->db->insert($this->getEntityTable(),$data); |
74
|
|
|
|
75
|
|
|
if (!$id = $this->db->insert_id()) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$Entity->setId($id); |
80
|
|
|
} |
81
|
|
|
//update |
82
|
|
|
else { |
83
|
|
|
|
84
|
|
|
if(!$this->getAdapter()->update($this->getEntityTable(), $data, "{$this->getPrimaryKey()} = '{$id}'")){ |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if(method_exists($this, 'onSaveSuccess' )){ return $this->onSaveSuccess( $Entity );} |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* из объекта формирует массив |
97
|
|
|
* @param \Core\Infrastructure\EntityInterface $Entity |
98
|
|
|
* @return \Core\Infrastructure\EntityInterface |
99
|
|
|
* @throws BadMethodCallException |
100
|
|
|
*/ |
101
|
|
|
protected function unbuildEntity(EntityInterface $Entity){ |
102
|
|
|
|
103
|
|
|
$mapfileds = array_merge([ 'id' => $this->key], $this->setMappingFields()); |
104
|
|
|
|
105
|
|
|
$row = []; |
106
|
|
|
|
107
|
|
|
foreach ($mapfileds as $propery => $field ) { |
108
|
|
|
|
109
|
|
|
$method_get = 'get' . ucfirst($propery); |
110
|
|
|
|
111
|
|
|
if(!method_exists($Entity, $method_get )){ |
112
|
|
|
throw new BadMethodCallException("Метод {$method_get} не определен"); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if(method_exists($this, 'onUnBuild'.$propery )){ |
116
|
|
|
$value = $this->{'onUnBuild'.$propery}( $Entity->{$method_get}() ); |
117
|
|
|
} |
118
|
|
|
else{ |
119
|
|
|
$value = $Entity->{$method_get}(); |
120
|
|
|
} |
121
|
|
|
$row[$field] = $value; |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $row; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Подготавливаем конечный вариант Сущности |
130
|
|
|
* |
131
|
|
|
* @param \Core\Infrastructure\EntityInterface $Entity |
132
|
|
|
* @param array $row |
133
|
|
|
* @return \Core\Infrastructure\EntityInterface |
134
|
|
|
* @throws BadMethodCallException |
135
|
|
|
*/ |
136
|
|
|
protected function buildEntity(EntityInterface $Entity, array $row){ |
137
|
|
|
|
138
|
|
|
$mapfields = array_merge([ 'id' => $this->key], $this->setMappingFields()); |
139
|
|
|
|
140
|
|
|
foreach ($mapfields as $propery => $field ) { |
141
|
|
|
|
142
|
|
|
$value = false; |
143
|
|
|
|
144
|
|
|
$method_set = 'set' . ucfirst($propery); |
145
|
|
|
|
146
|
|
|
if(!method_exists($Entity, $method_set )){ |
147
|
|
|
throw new BadMethodCallException("Метод {$method_set} не определен"); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
//событие onBuildField |
151
|
|
|
if(method_exists($this, 'onBuild'.$propery )){ |
152
|
|
|
$value = $this->{'onBuild'.$propery}($field,$row); |
153
|
|
|
} |
154
|
|
View Code Duplication |
elseif(is_string($field) && isset($row[strtolower($field)])){ |
|
|
|
|
155
|
|
|
$value = $row[strtolower($field)]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if($value!==false) |
159
|
|
|
$Entity->{$method_set}($value); |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $Entity; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
abstract protected function setEntityTable(); |
168
|
|
|
|
169
|
|
|
abstract protected function getPrimaryKey(); |
170
|
|
|
|
171
|
|
|
abstract protected function setMappingFields(); |
172
|
|
|
|
173
|
|
|
abstract protected function setSoftDeleteKey(); |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* |
177
|
|
|
* @param \Core\Infrastructure\ISpecificationCriteria $specification |
178
|
|
|
* @return type |
179
|
|
|
*/ |
180
|
|
|
public function findBySpecification(ISpecificationCriteria $specification){ |
181
|
|
|
|
182
|
|
|
//псеводо удаление |
183
|
|
|
$this->setSoftDelete($specification); |
184
|
|
|
|
185
|
|
|
$this->setRelations($specification); |
186
|
|
|
|
187
|
|
|
$specification->setLimit(1); |
188
|
|
|
|
189
|
|
|
//получение записей |
190
|
|
|
$res = $this->getAdapter()->getResultQuery($this->getEntityTable(),$specification); |
191
|
|
|
|
192
|
|
|
if (!$row = $res->row_array()) { |
193
|
|
|
return null; |
194
|
|
|
} |
195
|
|
|
return $this->createEntity($row); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Удаление записи |
200
|
|
|
* @param \Core\Infrastructure\EntityInterface $Entity |
201
|
|
|
* @return boolean |
202
|
|
|
*/ |
203
|
|
|
public function delete(EntityInterface $Entity) |
204
|
|
|
{ |
205
|
|
|
$result = false; |
206
|
|
|
|
207
|
|
|
$delete_key = $this->setSoftDeleteKey(); |
208
|
|
|
|
209
|
|
|
if ( |
210
|
|
|
$delete_key > '' && |
211
|
|
|
$Entity->getId() > 0){ |
212
|
|
|
$result = $this->db->update($this->getEntityTable(), [ $delete_key => 1 ], "{$this->getPrimaryKey()} = '{$Entity->getId()}'"); |
213
|
|
|
} |
214
|
|
|
elseif($Entity->getId() > 0){ |
215
|
|
|
|
216
|
|
|
if($result = $this->db->delete($this->getEntityTable(), $this->getPrimaryKey()." = ".$Entity->getId())){ |
|
|
|
|
217
|
|
|
if(method_exists($this, 'onDeleteSuccess' )){ $result = $this->onDeleteSuccess( $Entity );} |
|
|
|
|
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $result; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function findAllBySpecification(ISpecificationCriteria $specification) |
225
|
|
|
{ |
226
|
|
|
|
227
|
|
|
$entities = []; |
|
|
|
|
228
|
|
|
|
229
|
|
|
//псеводо удаление |
230
|
|
|
$this->setSoftDelete($specification); |
231
|
|
|
|
232
|
|
|
$this->setRelations($specification); |
233
|
|
|
|
234
|
|
|
$res = $this->getAdapter()->getResultQuery($this->getEntityTable(),$specification); |
235
|
|
|
|
236
|
|
|
if (!$rows = $res->result_array()) { |
237
|
|
|
return null; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
foreach($rows as $k => $row){ |
241
|
|
|
$rows[$k] = $this->createEntity($row); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $rows; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function findAll() |
248
|
|
|
{ |
249
|
|
|
return $this->findAllBySpecification(new Specification()); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Выборка удаленных моделей |
254
|
|
|
* @param \Core\Infrastructure\ISpecificationCriteria $specification |
255
|
|
|
*/ |
256
|
|
|
private function setSoftDelete(ISpecificationCriteria $specification){ |
257
|
|
|
if( |
258
|
|
|
$this->use_delete === false && |
259
|
|
|
$this->setSoftDeleteKey()>'' |
260
|
|
|
&& !isset($specification->getWhere()[$this->setSoftDeleteKey()]) |
261
|
|
|
) |
262
|
|
|
$specification->setWhere($this->setSoftDeleteKey(),0); |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Построение join-ов |
267
|
|
|
*/ |
268
|
|
|
protected function setRelations(ISpecificationCriteria $Specification){ |
269
|
|
|
if($this->use_joins===true){ |
270
|
|
|
$joins = []; |
271
|
|
|
|
272
|
|
|
foreach($this->setJoins() as $join){ |
|
|
|
|
273
|
|
|
$table = (new $join['mapper']($this->getAdapter()))->setEntityTable(); |
274
|
|
|
|
275
|
|
|
$joins[$table] = $join; |
276
|
|
|
|
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
$Specification->setJoins($joins); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Использование join-ов |
285
|
|
|
*/ |
286
|
|
|
public function useJoins() |
287
|
|
|
{ |
288
|
|
|
$o = clone $this; |
289
|
|
|
$o->use_joins = true; |
|
|
|
|
290
|
|
|
return $o; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function withDelete(){ |
294
|
|
|
$o = clone $this; |
295
|
|
|
$o->use_delete = true; |
296
|
|
|
return $o; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
} |
300
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.