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 TraitDataMapperEvent |
12
|
|
|
* |
13
|
|
|
* @author d.lanec |
14
|
|
|
*/ |
15
|
|
|
trait TraitDataMapperEvent { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Перед сохранением извелкаем объект и дополняем массив для записи, недостающими полями |
19
|
|
|
* @param \Autoprice\Domain\Price\EntityInterface $Entity |
20
|
|
|
* @param type $data |
21
|
|
|
*/ |
22
|
|
|
protected function onPrepareData(\SimpleORM\EntityInterface $Entity, &$data) { |
23
|
|
|
foreach ($this->mapping_fields as $field => $cfg) { |
24
|
|
|
if (isset($cfg['null']) && $cfg['null'] === false && empty($data[$cfg['field']])) { |
25
|
|
|
$data[$cfg['field']] = $cfg['default']; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* На успешное удаление |
32
|
|
|
* @param \SimpleORM\EntityInterface $Entity |
33
|
|
|
*/ |
34
|
|
|
protected function onBeforeDelete(EntityInterface $Entity) { |
35
|
|
|
foreach ($this->relations as $alias => $cfg) { |
36
|
|
|
$mapper = $cfg['mapper']; |
37
|
|
|
//если связь один к одному то удаляем сущность |
38
|
|
|
if ($cfg['reltype'] == 'has_one') { |
39
|
|
|
$Entity = $Entity->{'get' . $alias}(); |
40
|
|
|
if (!$mapper->delete($Entity)) { |
41
|
|
|
throw new \Autoprice\Exceptions\EntityNotDeleteException('Unable to delete Entity!'); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Событие перед сохранением |
51
|
|
|
* @param \SimpleORM\EntityInterface $Entity |
52
|
|
|
*/ |
53
|
|
|
protected function onAfterSave(EntityInterface $Entity) { |
54
|
|
|
|
55
|
|
|
$this->getAdapter()->startTransaction(); |
56
|
|
|
|
57
|
|
|
$rel_list = $this->createListRelation(); |
58
|
|
|
|
59
|
|
|
foreach ($rel_list as $obj_path => $mapper) { |
60
|
|
|
|
61
|
|
|
$call_obj = '$Entity'.$obj_path.';'; |
62
|
|
|
|
63
|
|
|
$set_path = str_replace(['#', '();'], ['->set', '($o);'], $call_obj); |
64
|
|
|
|
65
|
|
|
$ar_path = explode('()',$obj_path); |
66
|
|
|
|
67
|
|
|
$o = $Entity; |
68
|
|
|
|
69
|
|
|
foreach ($ar_path as $_m){ |
70
|
|
|
|
71
|
|
|
$_mc = str_replace('#','get',ucfirst($_m)); |
72
|
|
|
|
73
|
|
|
//Set logic |
74
|
|
|
if(empty($_m)){ |
75
|
|
|
|
76
|
|
|
$_mc = ltrim( $ar_path[(count($ar_path)-2)] , '#'); |
77
|
|
|
|
78
|
|
|
if (isset($$_mc) && is_object($$_mc) && is_a($$_mc,'SimpleORM\EntityInterface') && $this->DI->get($mapper)->saveWithoutEvents($o)) { |
79
|
|
|
$o = $$_mc; |
80
|
|
|
eval($set_path); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
elseif(is_object($o) ){ |
84
|
|
|
$$_mc = $o->{$_mc}(); |
85
|
|
|
$o = $$_mc; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* После успешного сохранения |
95
|
|
|
* @param \SimpleORM\EntityInterface $Entity |
96
|
|
|
*/ |
97
|
|
|
protected function onBeforeSave(EntityInterface $Entity) { |
98
|
|
|
|
99
|
|
|
$this->getAdapter()->endTransaction(); |
100
|
|
|
|
101
|
|
|
// foreach ($this->relations as $alias => $mapper) { |
102
|
|
|
// |
103
|
|
|
// $SaveEntity = $Entity->{'get'.$alias}(); |
104
|
|
|
// |
105
|
|
|
// if(!$mapper->save($SaveEntity)){ |
106
|
|
|
// throw new \Autoprice\Exceptions\EntityNotSaveException('Unable to save Entity!'); |
107
|
|
|
// } |
108
|
|
|
// |
109
|
|
|
// unset($SaveEntity); |
110
|
|
|
// } |
111
|
|
|
// |
112
|
|
|
// return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
On one hand,
eval
might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM,eval
prevents some optimization that they perform.