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
|
|
|
|
25
|
|
|
if ($cfg['null'] === false && empty($data[$cfg['field']])) { |
26
|
|
|
$data[$cfg['field']] = $cfg['default']; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* На успешное удаление |
33
|
|
|
* @param \SimpleORM\EntityInterface $Entity |
34
|
|
|
*/ |
35
|
|
|
protected function onBeforeDelete(EntityInterface $Entity) { |
36
|
|
|
foreach ($this->relations as $alias => $cfg) { |
|
|
|
|
37
|
|
|
$mapper = $cfg['mapper']; |
38
|
|
|
//если связь один к одному то удаляем сущность |
39
|
|
|
if ($cgg['reltype'] == 'has_one') { |
|
|
|
|
40
|
|
|
$Entity = $Entity->{'get' . $alias}(); |
41
|
|
|
if (!$mapper->delete($Entity)) { |
42
|
|
|
throw new \Autoprice\Exceptions\EntityNotDeleteException('Unable to delete Entity!'); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Событие перед сохранением |
52
|
|
|
* @param \SimpleORM\EntityInterface $Entity |
53
|
|
|
*/ |
54
|
|
|
protected function onAfterSave(EntityInterface $Entity) { |
55
|
|
|
|
56
|
|
|
$this->getAdapter()->startTransaction(); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$rel_list = $this->createListRelation(); |
|
|
|
|
59
|
|
|
|
60
|
|
|
foreach ($rel_list as $obj_path => $mapper) { |
61
|
|
|
|
62
|
|
|
$call_obj = '$Entity'.$obj_path.';'; |
63
|
|
|
|
64
|
|
|
$set_path = str_replace(['#', '();'], ['->set', '($o);'], $call_obj); |
65
|
|
|
|
66
|
|
|
$ar_path = explode('()',$obj_path); |
67
|
|
|
|
68
|
|
|
$o = $Entity; |
69
|
|
|
|
70
|
|
|
foreach ($ar_path as $_m){ |
71
|
|
|
|
72
|
|
|
$_mc = str_replace('#','get',mb_ucfirst($_m)); |
73
|
|
|
|
74
|
|
|
//Set logic |
75
|
|
|
if(empty($_m)){ |
76
|
|
|
|
77
|
|
|
$_mc = ltrim( $ar_path[(count($ar_path)-2)] , '#'); |
78
|
|
|
|
79
|
|
|
if (is_object($$_mc) && is_a($$_mc,'SimpleORM\EntityInterface') && $this->DI->get($mapper)->saveWithoutEvents($o)) { |
|
|
|
|
80
|
|
|
$o = $$_mc; |
81
|
|
|
eval($set_path); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
elseif(is_object($o) ){ |
85
|
|
|
$$_mc = $o->{$_mc}(); |
86
|
|
|
$o = $$_mc; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* После успешного сохранения |
96
|
|
|
* @param \SimpleORM\EntityInterface $Entity |
97
|
|
|
*/ |
98
|
|
|
protected function onBeforeSave(EntityInterface $Entity) { |
|
|
|
|
99
|
|
|
|
100
|
|
|
$this->getAdapter()->endTransaction(); |
|
|
|
|
101
|
|
|
|
102
|
|
|
// foreach ($this->relations as $alias => $mapper) { |
|
|
|
|
103
|
|
|
// |
104
|
|
|
// $SaveEntity = $Entity->{'get'.$alias}(); |
105
|
|
|
// |
106
|
|
|
// if(!$mapper->save($SaveEntity)){ |
107
|
|
|
// throw new \Autoprice\Exceptions\EntityNotSaveException('Unable to save Entity!'); |
108
|
|
|
// } |
109
|
|
|
// |
110
|
|
|
// unset($SaveEntity); |
111
|
|
|
// } |
112
|
|
|
// |
113
|
|
|
// return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.