|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
namespace Doctrine\ORM\Event; |
|
|
4 |
|
|
|
5 |
use Doctrine\Common\EventArgs, |
|
|
6 |
Doctrine\ORM\EntityManager; |
|
|
7 |
|
|
|
8 |
/** |
|
|
9 |
* Class that holds event arguments for a preInsert/preUpdate event. |
|
|
10 |
* |
|
|
11 |
* @author Roman Borschel <roman@code-factory.org> |
|
|
12 |
* @author Benjamin Eberlei <kontakt@beberlei.de> |
|
|
13 |
* @since 2.0 |
|
|
14 |
*/ |
|
|
15 |
class PreUpdateEventArgs extends LifecycleEventArgs |
|
|
16 |
{ |
|
|
17 |
/** |
|
|
18 |
* @var array |
|
|
19 |
*/ |
|
|
20 |
private $_entityChangeSet; |
|
|
21 |
|
|
|
22 |
/** |
|
|
23 |
* |
|
|
24 |
* @param object $entity |
|
|
25 |
* @param EntityManager $em |
|
|
26 |
* @param array $changeSet |
|
|
27 |
*/ |
|
|
28 |
public function __construct($entity, $em, array &$changeSet) |
|
|
29 |
{ |
|
|
30 |
parent::__construct($entity, $em); |
|
|
31 |
$this->_entityChangeSet = &$changeSet; |
|
|
32 |
} |
|
|
33 |
|
|
|
34 |
public function getEntityChangeSet() |
|
|
35 |
{ |
|
|
36 |
return $this->_entityChangeSet; |
|
|
37 |
} |
|
|
38 |
|
|
|
39 |
/** |
|
|
40 |
* Field has a changeset? |
|
|
41 |
* |
|
|
42 |
* @return bool |
|
|
43 |
*/ |
|
|
44 |
public function hasChangedField($field) |
|
|
45 |
{ |
|
|
46 |
return isset($this->_entityChangeSet[$field]); |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
/** |
|
|
50 |
* Get the old value of the changeset of the changed field. |
|
|
51 |
* |
|
|
52 |
* @param string $field |
|
|
53 |
* @return mixed |
|
|
54 |
*/ |
|
|
55 |
public function getOldValue($field) |
|
|
56 |
{ |
|
|
57 |
$this->_assertValidField($field); |
|
|
58 |
|
|
|
59 |
return $this->_entityChangeSet[$field][0]; |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
/** |
|
|
63 |
* Get the new value of the changeset of the changed field. |
|
|
64 |
* |
|
|
65 |
* @param string $field |
|
|
66 |
* @return mixed |
|
|
67 |
*/ |
|
|
68 |
public function getNewValue($field) |
|
|
69 |
{ |
|
|
70 |
$this->_assertValidField($field); |
|
|
71 |
|
|
|
72 |
return $this->_entityChangeSet[$field][1]; |
|
|
73 |
} |
|
|
74 |
|
|
|
75 |
/** |
|
|
76 |
* Set the new value of this field. |
|
|
77 |
* |
|
|
78 |
* @param string $field |
|
|
79 |
* @param mixed $value |
|
|
80 |
*/ |
|
|
81 |
public function setNewValue($field, $value) |
|
|
82 |
{ |
|
|
83 |
$this->_assertValidField($field); |
|
|
84 |
|
|
|
85 |
$this->_entityChangeSet[$field][1] = $value; |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
private function _assertValidField($field) |
|
|
89 |
{ |
|
|
90 |
if (!isset($this->_entityChangeSet[$field])) { |
|
|
91 |
throw new \InvalidArgumentException( |
|
|
92 |
"Field '".$field."' is not a valid field of the entity ". |
|
|
93 |
"'".get_class($this->getEntity())."' in PreInsertUpdateEventArgs." |
|
|
94 |
); |
|
|
95 |
} |
|
|
96 |
} |
|
|
97 |
} |
|
|
98 |
|