1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* To change this license header, choose License Headers in Project Properties. |
4
|
|
|
* To change this template file, choose Tools | Templates |
5
|
|
|
* and open the template in the editor. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SimpleORM; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Description of AbstractOptDataMapper |
12
|
|
|
* |
13
|
|
|
* @author d.lanec |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractOptDataMapper extends AbstractDataMapper{ |
16
|
|
|
|
17
|
|
|
protected $soft_delete_key; |
18
|
|
|
|
19
|
|
|
protected $key; |
20
|
|
|
|
21
|
|
|
protected $table; |
22
|
|
|
|
23
|
|
|
protected $mapping_fields; |
24
|
|
|
|
25
|
|
|
protected $relations = []; |
26
|
|
|
|
27
|
|
|
protected $DI; |
28
|
|
|
|
29
|
|
|
function __construct(\League\Container\Container $DI, QueryBuilderInterface $adapter, $db_name = null) { |
|
|
|
|
30
|
|
|
|
31
|
|
|
$this->DI = $DI; |
32
|
|
|
|
33
|
|
|
$this->setMappingFields(); |
34
|
|
|
|
35
|
|
|
parent::__construct($adapter, $db_name); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Установка поля для маппинга |
40
|
|
|
*/ |
41
|
|
|
protected function addMappingField($alias,$field = null){ |
42
|
|
|
|
43
|
|
|
if(! isset($field['field'])){ |
44
|
|
|
$field['field'] = is_string($field)?$field:$alias; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->mapping_fields[$alias] = $field; |
48
|
|
|
|
49
|
|
|
if(isset($field['primary']) && $field['primary']===true){ |
50
|
|
|
$this->key = $field['field']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if(isset($field['softdelete']) && $field['softdelete']===true){ |
54
|
|
|
$this->soft_delete_key = $field['field']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Уставнока таблицы |
62
|
|
|
*/ |
63
|
|
|
protected function setEntityTable() { |
64
|
|
|
return $this->table; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Установка ключа |
69
|
|
|
*/ |
70
|
|
|
protected function getPrimaryKey() { |
71
|
|
|
return $this->key; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Устанвка поля для мягкого удаляения |
76
|
|
|
*/ |
77
|
|
|
protected function setSoftDeleteKey() { |
78
|
|
|
return $this->soft_delete_key; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Подготавливаем конечный вариант Сущности |
83
|
|
|
* |
84
|
|
|
* @param \Core\Infrastructure\EntityInterface $Entity |
85
|
|
|
* @param array $row |
86
|
|
|
* @return \Core\Infrastructure\EntityInterface |
87
|
|
|
* @throws BadMethodCallException |
88
|
|
|
*/ |
89
|
|
|
protected function buildEntity(EntityInterface $Entity, array $row){ |
90
|
|
|
|
91
|
|
|
foreach ($this->mapping_fields as $alias => $cfg ) { |
92
|
|
|
|
93
|
|
|
$value = false; |
94
|
|
|
|
95
|
|
|
$field = $cfg['field']; |
96
|
|
|
|
97
|
|
|
$method_set = 'set' . ucfirst($alias); |
98
|
|
|
|
99
|
|
|
if(!method_exists($Entity, $method_set )){ |
100
|
|
|
throw new BadMethodCallException("Метод {$method_set} не определен"); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
//событие на формирование поля |
104
|
|
|
if( isset($cfg['build']) && is_object($cfg['build']) ){ |
105
|
|
|
$value = call_user_func($cfg['build'], $row); |
106
|
|
|
} |
107
|
|
|
//на связь |
108
|
|
|
elseif(isset($cfg['relation'])){ |
109
|
|
|
|
110
|
|
|
$mapper = $this->DI->get($cfg['relation']); |
111
|
|
|
|
112
|
|
|
if($this->use_joins===true){ |
113
|
|
|
$value = $mapper->createEntity($row); |
114
|
|
|
} |
115
|
|
|
else{ |
116
|
|
|
$fkey = $mapper->key; |
117
|
|
|
$value = $mapper->findBySpecification((new Specification)->setWhere($fkey, $row[$field])); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
View Code Duplication |
elseif(is_string($field) && isset($row[strtolower($field)])){ |
|
|
|
|
122
|
|
|
$value = $row[strtolower($field)]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if($value!==false) |
126
|
|
|
$Entity->{$method_set}($value); |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $Entity; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* из объекта формирует массив |
136
|
|
|
* @param \Core\Infrastructure\EntityInterface $Entity |
137
|
|
|
* @return \Core\Infrastructure\EntityInterface |
138
|
|
|
* @throws BadMethodCallException |
139
|
|
|
*/ |
140
|
|
|
protected function unbuildEntity(EntityInterface $Entity){ |
141
|
|
|
|
142
|
|
|
$row = []; |
143
|
|
|
|
144
|
|
|
foreach ($this->mapping_fields as $alias => $cfg ) { |
145
|
|
|
|
146
|
|
|
$field = $cfg['field']; |
147
|
|
|
|
148
|
|
|
$method_get = 'get' . ucfirst($alias); |
149
|
|
|
|
150
|
|
|
if(!method_exists($Entity, $method_get )){ |
151
|
|
|
throw new BadMethodCallException("Метод {$method_get} не определен"); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
//-------------------------------------------------------------------- |
155
|
|
|
if( isset($cfg['unbuild']) && is_object($cfg['unbuild']) ){ |
156
|
|
|
$value = call_user_func($cfg['unbuild'], $Entity->{$method_get}() ); |
157
|
|
|
} |
158
|
|
|
elseif(isset($cfg['relation'])){ |
159
|
|
|
|
160
|
|
|
$value = $Entity->{$method_get}()->getId(); |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
else{ |
164
|
|
|
$value = $Entity->{$method_get}(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$row[$field] = $value; |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $row; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Построение join-ов |
177
|
|
|
*/ |
178
|
|
|
protected function setRelations(ISpecificationCriteria $Specification){ |
179
|
|
|
|
180
|
|
|
$joins = []; |
181
|
|
|
|
182
|
|
|
foreach ($this->mapping_fields as $field => $cfg){ |
183
|
|
|
if(isset($cfg['relation'])){ |
184
|
|
|
|
185
|
|
|
$this->relations[$field] = $mapper = $this->DI->get($cfg['relation']); |
186
|
|
|
|
187
|
|
|
$table = $mapper->getEntityTable(); |
188
|
|
|
|
189
|
|
|
$joins[$table] = [ |
190
|
|
|
'alias' => $field, |
191
|
|
|
//'type' => 'INNER', |
|
|
|
|
192
|
|
|
'on' => "{$this->table}.{$this->key} = {$field}.{$mapper->key}" |
193
|
|
|
]; |
194
|
|
|
|
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if($this->use_joins===true){ |
199
|
|
|
$Specification->setJoins($joins); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* На успешное сохранение |
205
|
|
|
* @param \SimpleORM\EntityInterface $Entity |
206
|
|
|
*/ |
207
|
|
View Code Duplication |
protected function onSaveSuccess(EntityInterface $Entity){ |
|
|
|
|
208
|
|
|
|
209
|
|
|
|
210
|
|
|
foreach ($this->relations as $alias => $mapper) { |
211
|
|
|
$Entity = $Entity->{'get'.$alias}(); |
212
|
|
|
if(!$mapper->save($Entity)){ |
213
|
|
|
throw new \Autoprice\Exceptions\EntityNotSaveException('Unable to save Entity!'); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return true; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* На успешное удаление |
222
|
|
|
* @param \SimpleORM\EntityInterface $Entity |
223
|
|
|
*/ |
224
|
|
View Code Duplication |
protected function onDeleteSuccess(EntityInterface $Entity) { |
|
|
|
|
225
|
|
|
foreach ($this->relations as $alias => $mapper) { |
226
|
|
|
$Entity = $Entity->{'get'.$alias}(); |
227
|
|
|
if(!$mapper->delete($Entity)){ |
228
|
|
|
throw new \Autoprice\Exceptions\EntityNotDeleteException('Unable to delete Entity!'); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return true; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.