|
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
|
|
|
$get_path = str_replace('#', '->get', '$o = $Entity' . $obj_path . ';'); |
|
63
|
|
|
$set_path = str_replace(['#', '();'], ['->set', '($o);'], '$Entity' . $obj_path . ';'); |
|
64
|
|
|
|
|
65
|
|
|
eval($get_path); //получаем объект таким образом дабы не гулять по корневому объекту |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
if (is_object($o) && is_a($o,'SimpleORM\EntityInterface') && $this->DI->get($mapper)->saveWithoutEvents($o)) { |
|
|
|
|
|
|
68
|
|
|
eval($set_path); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
unset($o); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* После успешного сохранения |
|
76
|
|
|
* @param \SimpleORM\EntityInterface $Entity |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function onBeforeSave(EntityInterface $Entity) { |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$this->getAdapter()->endTransaction(); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
// foreach ($this->relations as $alias => $mapper) { |
|
|
|
|
|
|
83
|
|
|
// |
|
84
|
|
|
// $SaveEntity = $Entity->{'get'.$alias}(); |
|
85
|
|
|
// |
|
86
|
|
|
// if(!$mapper->save($SaveEntity)){ |
|
87
|
|
|
// throw new \Autoprice\Exceptions\EntityNotSaveException('Unable to save Entity!'); |
|
88
|
|
|
// } |
|
89
|
|
|
// |
|
90
|
|
|
// unset($SaveEntity); |
|
91
|
|
|
// } |
|
92
|
|
|
// |
|
93
|
|
|
// return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.