[!!!] [~TASK] BlogExample (Domain): The directory structure of the Domain has changed...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Mapper / DataMapper.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
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.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
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.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * A mapper to map database tables configured in $TCA on domain objects.
27 *
28 * @package Extbase
29 * @subpackage extbase
30 * @version $ID:$
31 */
32 class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
33
34 /**
35 * @var Tx_Extbase_Persistence_IdentityMap
36 */
37 protected $identityMap;
38
39 /**
40 * @var Tx_Extbase_Persistence_ManagerInterface
41 */
42 protected $persistenceManager;
43
44 /**
45 * A reference to the page select object providing methods to perform language and work space overlays
46 *
47 * @var t3lib_pageSelect
48 **/
49 protected $pageSelectObject;
50
51 /**
52 * Cached data maps
53 *
54 * @var array
55 **/
56 protected $dataMaps = array();
57
58 /**
59 * @var Tx_Extbase_Persistence_QueryFactoryInterface
60 */
61 protected $queryFactory;
62
63 /**
64 * The TYPO3 reference index object
65 *
66 * @var t3lib_refindex
67 **/
68 protected $referenceIndex;
69
70 /**
71 * Constructs a new mapper
72 *
73 */
74 public function __construct() {
75 $this->queryFactory = t3lib_div::makeInstance('Tx_Extbase_Persistence_QueryFactory');
76 }
77
78 /**
79 * Injects the identity map
80 *
81 * @param Tx_Extbase_Persistence_IdentityMap $identityMap
82 * @return void
83 */
84 public function injectIdentityMap(Tx_Extbase_Persistence_IdentityMap $identityMap) {
85 $this->identityMap = $identityMap;
86 }
87
88 /**
89 * Injects the persistence manager
90 *
91 * @param Tx_Extbase_Persistence_ManagerInterface $persistenceManager
92 * @return void
93 */
94 public function injectPersistenceManager(Tx_Extbase_Persistence_ManagerInterface $persistenceManager) {
95 $this->persistenceManager = $persistenceManager;
96 $this->QOMFactory = $this->persistenceManager->getBackend()->getQOMFactory();
97 }
98
99 /**
100 * Maps the (aggregate root) rows and registers them as reconstituted
101 * with the session.
102 *
103 * @param Tx_Extbase_Persistence_RowIteratorInterface $rows
104 * @return array
105 */
106 public function map($className, Tx_Extbase_Persistence_RowIteratorInterface $rows) {
107 $objects = array();
108 foreach ($rows as $row) {
109 $objects[] = $this->mapSingleRow($className, $row);
110 }
111 return $objects;
112 }
113
114 /**
115 * Maps a single node into the object it represents
116 *
117 * @param Tx_Extbase_Persistence_RowInterface $node
118 * @return object
119 */
120 protected function mapSingleRow($className, Tx_Extbase_Persistence_RowInterface $row) {
121 if ($this->identityMap->hasUid($className, $row['uid'])) {
122 $object = $this->identityMap->getObjectByUid($className, $row['uid']);
123 } else {
124 $object = $this->createEmptyObject($className);
125 $this->thawProperties($object, $row);
126 $this->identityMap->registerObject($object, $object->getUid());
127 $object->_memorizeCleanState();
128 }
129 return $object;
130 }
131
132 /**
133 * Creates a skeleton of the specified object
134 *
135 * @param string $className Name of the class to create a skeleton for
136 * @return object The object skeleton
137 * @internal
138 */
139 protected function createEmptyObject($className) {
140 // Note: The class_implements() function also invokes autoload to assure that the interfaces
141 // and the class are loaded. Would end up with __PHP_Incomplete_Class without it.
142 if (!in_array('Tx_Extbase_DomainObject_DomainObjectInterface', class_implements($className))) throw new Tx_Extbase_Object_Exception_CannotReconstituteObject('Cannot create empty instance of the class "' . $className . '" because it does not implement the Tx_Extbase_DomainObject_DomainObjectInterface.', 1234386924);
143 $object = unserialize('O:' . strlen($className) . ':"' . $className . '":0:{};');
144 return $object;
145 }
146
147 /**
148 * Sets the given properties on the object.
149 *
150 * @param Tx_Extbase_DomainObject_DomainObjectInterface $object The object to set properties on
151 * @param Tx_Extbase_Persistence_RowInterface $row
152 * @return void
153 */
154 protected function thawProperties(Tx_Extbase_DomainObject_DomainObjectInterface $object, Tx_Extbase_Persistence_RowInterface $row) {
155 $className = get_class($object);
156 $dataMap = $this->getDataMap($className);
157 $properties = $object->_getProperties();
158 $object->_setProperty('uid', $row['uid']);
159 foreach ($properties as $propertyName => $propertyValue) {
160 if (!$dataMap->isPersistableProperty($propertyName)) continue;
161 $columnMap = $dataMap->getColumnMap($propertyName);
162 $columnName = $columnMap->getColumnName();
163 $propertyValue = NULL;
164 $propertyType = $columnMap->getPropertyType();
165 switch ($propertyType) {
166 case Tx_Extbase_Persistence_PropertyType::STRING;
167 case Tx_Extbase_Persistence_PropertyType::DATE;
168 case Tx_Extbase_Persistence_PropertyType::LONG;
169 case Tx_Extbase_Persistence_PropertyType::DOUBLE;
170 case Tx_Extbase_Persistence_PropertyType::BOOLEAN;
171 if (isset($row[$columnName])) {
172 $rawPropertyValue = $row[$columnName];
173 $propertyValue = $dataMap->convertFieldValueToPropertyValue($propertyType, $rawPropertyValue);
174 }
175 break;
176 case (Tx_Extbase_Persistence_PropertyType::REFERENCE):
177 if (!is_null($row[$columnName])) {
178 $propertyValue = $this->mapRelatedObjects($object, $propertyName, $row, $columnMap);
179 } else {
180 $propertyValue = NULL;
181 }
182 break;
183 // FIXME we have an object to handle... -> exception
184 default:
185 // SK: We should throw an exception as this point as there was an undefined propertyType we can not handle.
186 if (isset($row[$columnName])) {
187 $property = $row[$columnName];
188 if (is_object($property)) {
189 $propertyValue = $this->mapObject($property);
190 // SK: THIS case can not happen I think. At least $this->mapObject() is not available.
191 } else {
192 // SK: This case does not make sense either. $this-mapSingleRow has a different signature
193 $propertyValue = $this->mapSingleRow($className, $property);
194 }
195 }
196 break;
197 }
198
199 $object->_setProperty($propertyName, $propertyValue);
200 }
201 }
202
203 /**
204 * Maps related objects to an ObjectStorage
205 *
206 * @param object $parentObject The parent object for the mapping result
207 * @param string $propertyName The target property name for the mapping result
208 * @param Tx_Extbase_Persistence_RowInterface $row The actual database row
209 * @param int $loadingStrategy The loading strategy; one of Tx_Extbase_Persistence_Mapper_ColumnMap::STRATEGY_*
210 * @return array|Tx_Extbase_Persistence_ObjectStorage|Tx_Extbase_Persistence_LazyLoadingProxy|another implementation of a loading strategy
211 */
212 protected function mapRelatedObjects(Tx_Extbase_DomainObject_AbstractEntity $parentObject, $propertyName, Tx_Extbase_Persistence_RowInterface $row, Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap) {
213 $dataMap = $this->getDataMap(get_class($parentObject));
214 $columnMap = $dataMap->getColumnMap($propertyName);
215 if ($columnMap->getLoadingStrategy() === Tx_Extbase_Persistence_Mapper_ColumnMap::STRATEGY_PROXY) {
216 // TODO Remove dependency to the loading strategy implementation
217 $result = t3lib_div::makeInstance('Tx_Extbase_Persistence_LazyLoadingProxy', $parentObject, $propertyName, $dataMap);
218 } else {
219 if ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_ONE) {
220 $query = $this->queryFactory->create($columnMap->getChildClassName(), FALSE);
221 $result = current($query->matching($query->withUid($row[$columnMap->getColumnName()]))->execute());
222 } elseif ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY) {
223 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
224 $query = $this->queryFactory->create($columnMap->getChildClassName(), FALSE);
225 $parentKeyFieldName = $columnMap->getParentKeyFieldName();
226 if (isset($parentKeyFieldName)) {
227 $objects = $query->matching($query->equals($columnMap->getParentKeyFieldName(), $parentObject->getUid()))->execute();
228 } else {
229 $propertyValue = $row[$propertyName];
230 $objects = $query->matching($query->withUid((int)$propertyValue))->execute();
231 }
232 foreach ($objects as $object) {
233 $objectStorage->attach($object);
234 }
235 $result = $objectStorage;
236 } elseif ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
237 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
238 $relationTableName = $columnMap->getRelationTableName();
239 $left = $this->QOMFactory->selector($relationTableName);
240 $childTableName = $columnMap->getChildTableName();
241 $right = $this->QOMFactory->selector($childTableName);
242 $joinCondition = $this->QOMFactory->equiJoinCondition($relationTableName, $columnMap->getChildKeyFieldName(), $childTableName, 'uid');
243 $source = $this->QOMFactory->join(
244 $left,
245 $right,
246 Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_JOIN_TYPE_INNER,
247 $joinCondition
248 );
249 $query = $this->queryFactory->create($columnMap->getChildClassName(), FALSE);
250 $query->setSource($source);
251 $objects = $query->matching($query->equals($columnMap->getParentKeyFieldName(), $parentObject->getUid()))->execute();
252 foreach ($objects as $object) {
253 $objectStorage->attach($object);
254 }
255 $result = $objectStorage;
256 }
257 }
258
259 return $result;
260 }
261
262 /**
263 * Delegates the call to the Data Map.
264 * Returns TRUE if the property is persistable (configured in $TCA)
265 *
266 * @param string $className The property name
267 * @param string $propertyName The property name
268 * @return boolean TRUE if the property is persistable (configured in $TCA)
269 */
270 public function isPersistableProperty($className, $propertyName) {
271 $dataMap = $this->getDataMap($className);
272 return $dataMap->isPersistableProperty($propertyName);
273 }
274
275 /**
276 * Returns a data map for a given class name
277 *
278 * @return Tx_Extbase_Persistence_Mapper_DataMap The data map
279 */
280 public function getDataMap($className) {
281 if (empty($this->dataMaps[$className])) {
282 // FIXME This is a costy for table name aliases -> implement a DataMapBuilder (knowing the aliases defined in $TCA)
283 $mapping = array();
284 $extbaseSettings = Tx_Extbase_Dispatcher::getSettings();
285 if (isset($extbaseSettings['classes'][$className]) && !empty($extbaseSettings['classes'][$className]['mapping']['tableName'])) {
286 $tableName = $extbaseSettings['classes'][$className]['mapping']['tableName'];
287 } else {
288 foreach (class_parents($className) as $parentClassName) {
289 if (isset($extbaseSettings['classes'][$parentClassName]) && !empty($extbaseSettings['classes'][$parentClassName]['mapping']['tableName'])) {
290 $tableName = $extbaseSettings['classes'][$parentClassName]['mapping']['tableName'];
291 break;
292 }
293 // TODO throw Exception
294 }
295 }
296 if (is_array($extbaseSettings['classes'][$parentClassName]['mapping']['columns'])) {
297 $mapping = $extbaseSettings['classes'][$parentClassName]['mapping']['columns'];
298 }
299
300 $dataMap = new Tx_Extbase_Persistence_Mapper_DataMap($className, $tableName, $mapping);
301 $this->dataMaps[$className] = $dataMap;
302 }
303 return $this->dataMaps[$className];
304 }
305
306 /**
307 * Returns the selector (table) name for a given class name.
308 *
309 * @param string $className
310 * @return string The selector name
311 */
312 public function convertClassNameToSelectorName($className) {
313 return $this->getDataMap($className)->getTableName();
314 }
315
316 }
317 ?>