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; |
11
|
|
|
|
12
|
|
|
use League\Event\EmitterInterface; |
13
|
|
|
use League\Event\ListenerInterface; |
14
|
|
|
use Slick\Common\Base; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Entity |
18
|
|
|
* |
19
|
|
|
* @package Slick\Orm |
20
|
|
|
* @readwrite |
21
|
|
|
*/ |
22
|
|
|
abstract class Entity extends Base implements EntityInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Saves current entity state |
27
|
|
|
* |
28
|
|
|
* Optionally saves only the partial data if $data argument is passed. If |
29
|
|
|
* no data is given al the field properties will be updated. |
30
|
|
|
* |
31
|
|
|
* @param array $data Partial data to save |
32
|
|
|
* |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
2 |
|
public function save(array $data = []) |
36
|
|
|
{ |
37
|
2 |
|
return $this->getMapper() |
38
|
2 |
|
->save($this, $data); |
39
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Deletes current entity from its storage |
44
|
|
|
* |
45
|
|
|
* @return self|$this|EntityInterface |
46
|
|
|
*/ |
47
|
|
|
public function delete() |
48
|
|
|
{ |
49
|
|
|
return $this->getMapper() |
50
|
|
|
->delete($this); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieves the data mapper for this entity |
55
|
|
|
*/ |
56
|
2 |
|
public function getMapper() |
57
|
|
|
{ |
58
|
2 |
|
return Orm::getMapper(get_class($this)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Add a listener for an event. |
63
|
|
|
* |
64
|
|
|
* The first parameter should be the event name, and the second should be |
65
|
|
|
* the event listener. It may implement the League\Event\ListenerInterface |
66
|
|
|
* or simply be "callable". In this case, the priority emitter also accepts |
67
|
|
|
* an optional third parameter specifying the priority as an integer. You |
68
|
|
|
* may use one of EmitterInterface predefined constants here if you want. |
69
|
|
|
* |
70
|
|
|
* @param string $event |
71
|
|
|
* @param ListenerInterface|callable $listener |
72
|
|
|
* @param int $priority |
73
|
|
|
* |
74
|
|
|
* @return EmitterInterface |
75
|
|
|
*/ |
76
|
|
|
public function addListener( |
77
|
|
|
$event, $listener, $priority = EmitterInterface::P_NORMAL |
78
|
|
|
) { |
79
|
|
|
return Orm::addListener($this, $event, $listener, $priority); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |