|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/orm package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Orm\Mapper; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Database\Sql\Insert; |
|
13
|
|
|
use Slick\Orm\EntityInterface; |
|
14
|
|
|
use Slick\Orm\Event\Delete; |
|
15
|
|
|
use Slick\Orm\Event\Save; |
|
16
|
|
|
use Slick\Orm\Orm; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* ORM entity event triggers methods |
|
20
|
|
|
* |
|
21
|
|
|
* @package Slick\Orm\Mapper |
|
22
|
|
|
* @author Filipe Silva <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
trait EventTriggers |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Triggers the before save event |
|
29
|
|
|
* |
|
30
|
|
|
* @param sql/Update|Sql/Insert $query |
|
|
|
|
|
|
31
|
|
|
* @param EntityInterface $entity |
|
32
|
|
|
* @param array $data |
|
33
|
|
|
* |
|
34
|
|
|
* @return Save |
|
35
|
|
|
*/ |
|
36
|
4 |
|
protected function triggerBeforeSave( |
|
37
|
|
|
$query, EntityInterface $entity, array $data |
|
38
|
|
|
) { |
|
39
|
|
|
$save = $query instanceof Insert |
|
40
|
4 |
|
? (new Save($entity, $data)) |
|
41
|
2 |
|
->setAction(Save::ACTION_BEFORE_INSERT) |
|
42
|
4 |
|
: (new Save($entity, $data)) |
|
43
|
4 |
|
->setAction(Save::ACTION_BEFORE_UPDATE); |
|
44
|
|
|
|
|
45
|
|
|
/** @var Save $save */ |
|
46
|
4 |
|
return Orm::getEmitter($this->getEntityClassName())->emit($save); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Triggers the after save event |
|
51
|
|
|
* |
|
52
|
|
|
* @param Save $saveEvent |
|
53
|
|
|
* @param EntityInterface $entity |
|
54
|
|
|
* |
|
55
|
|
|
* @return \League\Event\EventInterface|Save |
|
56
|
|
|
*/ |
|
57
|
4 |
|
protected function triggerAfterSave( |
|
58
|
|
|
Save $saveEvent, EntityInterface $entity |
|
59
|
|
|
) { |
|
60
|
4 |
|
$afterSave = clone $saveEvent; |
|
61
|
4 |
|
$action = $afterSave->getName() == Save::ACTION_BEFORE_INSERT |
|
62
|
4 |
|
? Save::ACTION_AFTER_INSERT |
|
63
|
4 |
|
: Save::ACTION_AFTER_UPDATE; |
|
64
|
|
|
|
|
65
|
4 |
|
$afterSave->setEntity($entity)->setAction($action); |
|
66
|
4 |
|
return Orm::getEmitter($this->getEntityClassName()) |
|
67
|
4 |
|
->emit($afterSave); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Triggers the before delete event |
|
72
|
|
|
* |
|
73
|
|
|
* @param EntityInterface $entity |
|
74
|
|
|
* |
|
75
|
|
|
* @return \League\Event\EventInterface|Delete |
|
76
|
|
|
*/ |
|
77
|
2 |
View Code Duplication |
protected function triggerBeforeDelete(EntityInterface $entity) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
2 |
|
$event = new Delete($entity); |
|
80
|
2 |
|
$event->setAction(Delete::ACTION_BEFORE_DELETE); |
|
81
|
2 |
|
return Orm::getEmitter($this->getEntityClassName()) |
|
82
|
2 |
|
->emit($event); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Triggers the after delete event |
|
87
|
|
|
* |
|
88
|
|
|
* @param EntityInterface $entity |
|
89
|
|
|
* |
|
90
|
|
|
* @return \League\Event\EventInterface|Delete |
|
91
|
|
|
*/ |
|
92
|
2 |
View Code Duplication |
protected function triggerAfterDelete(EntityInterface $entity) |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
2 |
|
$event = new Delete($entity); |
|
95
|
2 |
|
$event->setAction(Delete::ACTION_AFTER_DELETE); |
|
96
|
2 |
|
return Orm::getEmitter($this->getEntityClassName()) |
|
97
|
2 |
|
->emit($event); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
abstract public function getEntityClassName(); |
|
101
|
|
|
} |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.