2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * An abstract Entity. An Entity is an object fundamentally defined not by its attributes,
27 * but by a thread of continuity and identity (e.g. a person).
30 * @subpackage DomainObject
33 abstract class Tx_Extbase_DomainObject_AbstractEntity
extends Tx_Extbase_DomainObject_AbstractDomainObject
{
36 * @var An array holding the clean property values. Set right after reconstitution of the object
38 private $_cleanProperties = array();
41 * Register an object's clean state, e.g. after it has been reconstituted
44 * @param string $propertyName The name of the property to be memorized. If omitted all persistable properties are memorized.
47 public function _memorizeCleanState($propertyName = NULL) {
48 if ($propertyName !== NULL) {
49 $this->_memorizePropertyCleanState($propertyName);
51 $dataMapper = t3lib_div
::makeInstance('Tx_Extbase_Persistence_Mapper_DataMapper'); // singleton
52 $this->_cleanProperties
= array();
53 $properties = get_object_vars($this);
54 foreach ($properties as $propertyName => $propertyValue) {
55 if ($dataMapper->isPersistableProperty(get_class($this), $propertyName)) {
56 $this->_memorizePropertyCleanState($propertyName);
63 * Register an properties's clean state, e.g. after it has been reconstituted
66 * @param string $propertyName The name of the property to be memorized. If omittet all persistable properties are memorized.
69 public function _memorizePropertyCleanState($propertyName) {
70 $propertyValue = $this->$propertyName;
71 if (is_object($propertyValue)) {
72 $this->_cleanProperties
[$propertyName] = clone($propertyValue);
74 // We need to make sure the clone and the original object
75 // are identical when compared with == (see _isDirty()).
76 // After the cloning, the Domain Object will have the property
77 // "isClone" set to TRUE, so we manually have to set it to FALSE
78 // again. Possible fix: Somehow get rid of the "isClone" property,
79 // which is currently needed in Fluid.
80 if ($propertyValue instanceof Tx_Extbase_DomainObject_AbstractDomainObject
) {
81 $this->_cleanProperties
[$propertyName]->_setClone(FALSE);
84 $this->_cleanProperties
[$propertyName] = $propertyValue;
89 * Returns a hash map of clean properties and $values.
93 public function _getCleanProperties() {
94 return $this->_cleanProperties
;
98 * Returns the clean value of the given property. The returned value will be NULL if the clean state was not memorized before, or
99 * if the clean value is NULL.
101 * @param string $propertyName The name of the property to be memorized. If omittet all persistable properties are memorized.
102 * @return mixed The clean property value or NULL
104 public function _getCleanProperty($propertyName) {
105 if (is_array($this->_cleanProperties
)) {
106 return isset($this->_cleanProperties
[$propertyName]) ?
$this->_cleanProperties
[$propertyName] : NULL;
113 * Returns TRUE if the properties were modified after reconstitution
115 * @param string $propertyName An optional name of a property to be checked if its value is dirty
118 public function _isDirty($propertyName = NULL) {
119 if (empty($this->_cleanProperties
)) return TRUE;
120 // if (!is_array($this->_cleanProperties)) throw new Tx_Extbase_Persistence_Exception_CleanStateNotMemorized('The clean state of the object "' . get_class($this) . '" has not been memorized before asking _isDirty().', 1233309106);
121 if ($this->uid
!== NULL && $this->uid
!= $this->_cleanProperties
['uid']) throw new Tx_Extbase_Persistence_Exception_TooDirty('The uid "' . $this->uid
. '" has been modified, that is simply too much.', 1222871239);
123 if ($propertyName !== NULL) {
124 if (is_object($this->$propertyName)) {
125 // In case it is an object, we do a simple comparison (!=) as we want cloned objects to return the same values.
126 $result = $this->_cleanProperties
[$propertyName] != $this->$propertyName;
128 $result = $this->_cleanProperties
[$propertyName] !== $this->$propertyName;
131 foreach ($this->_cleanProperties
as $propertyName => $propertyValue) {
132 if (is_object($this->$propertyName)) {
133 // In case it is an object, we do a simple comparison (!=) as we want cloned objects to return the same values.
134 if ($this->$propertyName != $propertyValue) {
139 if ($this->$propertyName !== $propertyValue) {