fd55c123c4655e2faed4c3bc13179e11393830e1
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / Backend.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * This class is a backport of the corresponding class of TYPO3 Flow.
8 * All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * A persistence backend. This backend maps objects to the relational model of the storage backend.
32 * It persists all added, removed and changed objects.
33 */
34 class Backend implements \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface, \TYPO3\CMS\Core\SingletonInterface {
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
38 */
39 protected $session;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
43 */
44 protected $aggregateRootObjects;
45
46 /**
47 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
48 */
49 protected $visitedDuringPersistence;
50
51 /**
52 * @var \TYPO3\CMS\Extbase\Persistence\Generic\IdentityMap
53 */
54 protected $identityMap;
55
56 /**
57 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
58 */
59 protected $reflectionService;
60
61 /**
62 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface
63 */
64 protected $queryFactory;
65
66 /**
67 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
68 */
69 protected $qomFactory;
70
71 /**
72 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface
73 */
74 protected $storageBackend;
75
76 /**
77 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
78 */
79 protected $dataMapper;
80
81 /**
82 * The TYPO3 reference index object
83 *
84 * @var \TYPO3\CMS\Core\Database\ReferenceIndex
85 */
86 protected $referenceIndex;
87
88 /**
89 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
90 */
91 protected $configurationManager;
92
93 /**
94 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
95 */
96 protected $signalSlotDispatcher;
97
98 /**
99 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
100 */
101 protected $deletedObjects;
102
103 /**
104 * Constructs the backend
105 *
106 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
107 * @return void
108 */
109 public function __construct(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
110 $this->configurationManager = $configurationManager;
111 $this->referenceIndex = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\ReferenceIndex');
112 $this->deletedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
113 }
114
115 /**
116 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Session $session
117 * @return void
118 */
119 public function injectSession(\TYPO3\CMS\Extbase\Persistence\Generic\Session $session) {
120 $this->session = $session;
121 }
122
123 /**
124 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface $storageBackend
125 * @return void
126 */
127 public function injectStorageBackend(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface $storageBackend) {
128 $this->storageBackend = $storageBackend;
129 }
130
131 /**
132 * Injects the DataMapper to map nodes to objects
133 *
134 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper
135 * @return void
136 */
137 public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper) {
138 $this->dataMapper = $dataMapper;
139 }
140
141 /**
142 * Injects the identity map
143 *
144 * @param \TYPO3\CMS\Extbase\Persistence\Generic\IdentityMap $identityMap
145 * @return void
146 */
147 public function injectIdentityMap(\TYPO3\CMS\Extbase\Persistence\Generic\IdentityMap $identityMap) {
148 $this->identityMap = $identityMap;
149 }
150
151 /**
152 * Injects the Reflection Service
153 *
154 * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService
155 * @return void
156 */
157 public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService) {
158 $this->reflectionService = $reflectionService;
159 }
160
161 /**
162 * Injects the QueryFactory
163 *
164 * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface $queryFactory
165 * @return void
166 */
167 public function injectQueryFactory(\TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface $queryFactory) {
168 $this->queryFactory = $queryFactory;
169 }
170
171 /**
172 * Injects the QueryObjectModelFactory
173 *
174 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory $qomFactory
175 * @return void
176 */
177 public function injectQomFactory(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory $qomFactory) {
178 $this->qomFactory = $qomFactory;
179 }
180
181 /**
182 * @param \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher
183 */
184 public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher) {
185 $this->signalSlotDispatcher = $signalSlotDispatcher;
186 }
187
188 /**
189 * Returns the repository session
190 *
191 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Session
192 */
193 public function getSession() {
194 return $this->session;
195 }
196
197 /**
198 * Returns the Data Mapper
199 *
200 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
201 */
202 public function getDataMapper() {
203 return $this->dataMapper;
204 }
205
206 /**
207 * Returns the current QOM factory
208 *
209 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
210 */
211 public function getQomFactory() {
212 return $this->qomFactory;
213 }
214
215 /**
216 * Returns the current identityMap
217 *
218 * @return \TYPO3\CMS\Extbase\Persistence\Generic\IdentityMap
219 */
220 public function getIdentityMap() {
221 return $this->identityMap;
222 }
223
224 /**
225 * Returns the reflection service
226 *
227 * @return \TYPO3\CMS\Extbase\Reflection\ReflectionService
228 */
229 public function getReflectionService() {
230 return $this->reflectionService;
231 }
232
233 /**
234 * Returns the number of records matching the query.
235 *
236 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
237 * @return integer
238 * @api
239 */
240 public function getObjectCountByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
241 return $this->storageBackend->getObjectCountByQuery($query);
242 }
243
244 /**
245 * Returns the object data matching the $query.
246 *
247 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
248 * @return array
249 * @api
250 */
251 public function getObjectDataByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
252 return $this->storageBackend->getObjectDataByQuery($query);
253 }
254
255 /**
256 * Returns the (internal) identifier for the object, if it is known to the
257 * backend. Otherwise NULL is returned.
258 *
259 * @param object $object
260 * @return string The identifier for the object if it is known, or NULL
261 */
262 public function getIdentifierByObject($object) {
263 if ($object instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
264 $object = $object->_loadRealInstance();
265 if (!is_object($object)) {
266 return NULL;
267 }
268 }
269 if ($this->identityMap->hasObject($object)) {
270 return $this->identityMap->getIdentifierByObject($object);
271 } else {
272 return NULL;
273 }
274 }
275
276 /**
277 * Returns the object with the (internal) identifier, if it is known to the
278 * backend. Otherwise NULL is returned.
279 *
280 * @param string $identifier
281 * @param string $className
282 * @return object The object for the identifier if it is known, or NULL
283 */
284 public function getObjectByIdentifier($identifier, $className) {
285 if ($this->identityMap->hasIdentifier($identifier, $className)) {
286 return $this->identityMap->getObjectByIdentifier($identifier, $className);
287 } else {
288 $query = $this->queryFactory->create($className);
289 $query->getQuerySettings()->setRespectStoragePage(FALSE);
290 return $query->matching($query->equals('uid', $identifier))->execute()->getFirst();
291 }
292 }
293
294 /**
295 * Checks if the given object has ever been persisted.
296 *
297 * @param object $object The object to check
298 * @return boolean TRUE if the object is new, FALSE if the object exists in the repository
299 */
300 public function isNewObject($object) {
301 return $this->getIdentifierByObject($object) === NULL;
302 }
303
304 /**
305 * Replaces the given object by the second object.
306 *
307 * This method will unregister the existing object at the identity map and
308 * register the new object instead. The existing object must therefore
309 * already be registered at the identity map which is the case for all
310 * reconstituted objects.
311 *
312 * The new object will be identified by the uid which formerly belonged
313 * to the existing object. The existing object looses its uid.
314 *
315 * @param object $existingObject The existing object
316 * @param object $newObject The new object
317 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
318 * @return void
319 */
320 public function replaceObject($existingObject, $newObject) {
321 $existingUid = $this->getIdentifierByObject($existingObject);
322 if ($existingUid === NULL) {
323 throw new \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException('The given object is unknown to this persistence backend.', 1238070163);
324 }
325 $this->identityMap->unregisterObject($existingObject);
326 $this->identityMap->registerObject($newObject, $existingUid);
327 }
328
329 /**
330 * Sets the aggregate root objects
331 *
332 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $objects
333 * @return void
334 */
335 public function setAggregateRootObjects(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $objects) {
336 $this->aggregateRootObjects = $objects;
337 }
338
339 /**
340 * Sets the deleted objects
341 *
342 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $objects
343 * @return void
344 */
345 public function setDeletedObjects(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $objects) {
346 $this->deletedObjects = $objects;
347 }
348
349 /**
350 * Commits the current persistence session.
351 *
352 * @return void
353 */
354 public function commit() {
355 $this->persistObjects();
356 $this->processDeletedObjects();
357 }
358
359 /**
360 * Traverse and persist all aggregate roots and their object graph.
361 *
362 * @return void
363 */
364 protected function persistObjects() {
365 $this->visitedDuringPersistence = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
366 foreach ($this->aggregateRootObjects as $object) {
367 if (!$this->identityMap->hasObject($object)) {
368 $this->insertObject($object);
369 }
370 }
371 foreach ($this->aggregateRootObjects as $object) {
372 $this->persistObject($object);
373 }
374 }
375
376 /**
377 * Persists the given object.
378 *
379 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be inserted
380 * @return void
381 */
382 protected function persistObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object) {
383 if (isset($this->visitedDuringPersistence[$object])) {
384 return;
385 }
386 $row = array();
387 $queue = array();
388 $dataMap = $this->dataMapper->getDataMap(get_class($object));
389 $properties = $object->_getProperties();
390 foreach ($properties as $propertyName => $propertyValue) {
391 if (!$dataMap->isPersistableProperty($propertyName) || $this->propertyValueIsLazyLoaded($propertyValue)) {
392 continue;
393 }
394 $columnMap = $dataMap->getColumnMap($propertyName);
395 if ($propertyValue instanceof \TYPO3\CMS\Extbase\Persistence\ObjectStorage) {
396 if ($object->_isNew() || $propertyValue->_isDirty()) {
397 $this->persistObjectStorage($propertyValue, $object, $propertyName, $row);
398 $propertyValue->_memorizeCleanState();
399 }
400 foreach ($propertyValue as $containedObject) {
401 if ($containedObject instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
402 $queue[] = $containedObject;
403 }
404 }
405 } elseif ($propertyValue instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
406 if ($object->_isDirty($propertyName)) {
407 if ($propertyValue->_isNew()) {
408 $this->insertObject($propertyValue);
409 }
410 $row[$columnMap->getColumnName()] = $this->getPlainValue($propertyValue);
411 }
412 $queue[] = $propertyValue;
413 } elseif ($object->_isNew() || $object->_isDirty($propertyName)) {
414 $row[$columnMap->getColumnName()] = $this->getPlainValue($propertyValue, $columnMap);
415 }
416 }
417 if (count($row) > 0) {
418 $this->updateObject($object, $row);
419 $object->_memorizeCleanState();
420 }
421 $this->visitedDuringPersistence[$object] = $object->getUid();
422 foreach ($queue as $queuedObject) {
423 $this->persistObject($queuedObject);
424 }
425 }
426
427 /**
428 * Checks, if the property value is lazy loaded and was not initialized
429 *
430 * @param mixed $propertyValue The property value
431 * @return bool
432 */
433 protected function propertyValueIsLazyLoaded($propertyValue) {
434 if ($propertyValue instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
435 return TRUE;
436 }
437 if ($propertyValue instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage) {
438 if ($propertyValue->isInitialized() === FALSE) {
439 return TRUE;
440 }
441 }
442 return FALSE;
443 }
444
445 /**
446 * Persists a an object storage. Objects of a 1:n or m:n relation are queued and processed with the parent object. A 1:1 relation
447 * gets persisted immediately. Objects which were removed from the property were detached from the parent object. They will not be
448 * deleted by default. You have to annotate the property with "@cascade remove" if you want them to be deleted as well.
449 *
450 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $objectStorage The object storage to be persisted.
451 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object. One of the properties holds the object storage.
452 * @param string $propertyName The name of the property holding the object storage.
453 * @param array $row The row array of the parent object to be persisted. It's passed by reference and gets filled with either a comma separated list of uids (csv) or the number of contained objects.
454 * @return void
455 */
456 protected function persistObjectStorage(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $objectStorage, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $propertyName, array &$row) {
457 $className = get_class($parentObject);
458 $columnMap = $this->dataMapper->getDataMap($className)->getColumnMap($propertyName);
459 $propertyMetaData = $this->reflectionService->getClassSchema($className)->getProperty($propertyName);
460 foreach ($this->getRemovedChildObjects($parentObject, $propertyName) as $removedObject) {
461 if ($columnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY && $propertyMetaData['cascade'] === 'remove') {
462 $this->removeObject($removedObject);
463 } else {
464 $this->detachObjectFromParentObject($removedObject, $parentObject, $propertyName);
465 }
466 }
467 if ($columnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
468 $this->deleteAllRelationsFromRelationtable($parentObject, $propertyName);
469 }
470 $currentUids = array();
471 $sortingPosition = 1;
472 foreach ($objectStorage as $object) {
473 if ($object->_isNew()) {
474 $this->insertObject($object);
475 }
476 $currentUids[] = $object->getUid();
477 $this->attachObjectToParentObject($object, $parentObject, $propertyName, $sortingPosition);
478 $sortingPosition++;
479 }
480 if ($columnMap->getParentKeyFieldName() === NULL) {
481 $row[$columnMap->getColumnName()] = implode(',', $currentUids);
482 } else {
483 $row[$columnMap->getColumnName()] = $this->dataMapper->countRelated($parentObject, $propertyName);
484 }
485 }
486
487 /**
488 * Returns the removed objects determined by a comparison of the clean property value
489 * with the actual property value.
490 *
491 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object
492 * @param string $propertyName
493 * @return array An array of removed objects
494 */
495 protected function getRemovedChildObjects(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, $propertyName) {
496 $removedObjects = array();
497 $cleanPropertyValue = $object->_getCleanProperty($propertyName);
498 if (is_array($cleanPropertyValue) || $cleanPropertyValue instanceof \Iterator) {
499 $propertyValue = $object->_getProperty($propertyName);
500 foreach ($cleanPropertyValue as $containedObject) {
501 if (!$propertyValue->contains($containedObject)) {
502 $removedObjects[] = $containedObject;
503 }
504 }
505 }
506 return $removedObjects;
507 }
508
509 /**
510 * Updates the fields defining the relation between the object and the parent object.
511 *
512 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
513 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject
514 * @param string $parentPropertyName
515 * @param integer $sortingPosition
516 * @return void
517 */
518 protected function attachObjectToParentObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName, $sortingPosition = 0) {
519 $parentDataMap = $this->dataMapper->getDataMap(get_class($parentObject));
520 $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
521 if ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY) {
522 $row = array();
523 $parentKeyFieldName = $parentColumnMap->getParentKeyFieldName();
524 if ($parentKeyFieldName !== NULL) {
525 $row[$parentKeyFieldName] = $parentObject->getUid();
526 $parentTableFieldName = $parentColumnMap->getParentTableFieldName();
527 if ($parentTableFieldName !== NULL) {
528 $row[$parentTableFieldName] = $parentDataMap->getTableName();
529 }
530 }
531 $childSortByFieldName = $parentColumnMap->getChildSortByFieldName();
532 if (!empty($childSortByFieldName)) {
533 $row[$childSortByFieldName] = $sortingPosition;
534 }
535 if (count($row) > 0) {
536 $this->updateObject($object, $row);
537 }
538 } elseif ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
539 $this->insertRelationInRelationtable($object, $parentObject, $parentPropertyName, $sortingPosition);
540 }
541 }
542
543 /**
544 * Updates the fields defining the relation between the object and the parent object.
545 *
546 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
547 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject
548 * @param string $parentPropertyName
549 * @return void
550 */
551 protected function detachObjectFromParentObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName) {
552 $parentDataMap = $this->dataMapper->getDataMap(get_class($parentObject));
553 $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
554 if ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY) {
555 $row = array();
556 $parentKeyFieldName = $parentColumnMap->getParentKeyFieldName();
557 if ($parentKeyFieldName !== NULL) {
558 $row[$parentKeyFieldName] = '';
559 $parentTableFieldName = $parentColumnMap->getParentTableFieldName();
560 if ($parentTableFieldName !== NULL) {
561 $row[$parentTableFieldName] = '';
562 }
563 }
564 $childSortByFieldName = $parentColumnMap->getChildSortByFieldName();
565 if (!empty($childSortByFieldName)) {
566 $row[$childSortByFieldName] = 0;
567 }
568 if (count($row) > 0) {
569 $this->updateObject($object, $row);
570 }
571 } elseif ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
572 $this->deleteRelationFromRelationtable($object, $parentObject, $parentPropertyName);
573 }
574 }
575
576 /**
577 * Inserts an object in the storage backend
578 *
579 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be insterted in the storage
580 * @return void
581 */
582 protected function insertObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object) {
583 if ($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject) {
584 $result = $this->getUidOfAlreadyPersistedValueObject($object);
585 if ($result !== FALSE) {
586 $object->_setProperty('uid', (integer) $result);
587 return;
588 }
589 }
590 $dataMap = $this->dataMapper->getDataMap(get_class($object));
591 $row = array();
592 $this->addCommonFieldsToRow($object, $row);
593 if ($dataMap->getLanguageIdColumnName() !== NULL) {
594 $row[$dataMap->getLanguageIdColumnName()] = -1;
595 }
596 $uid = $this->storageBackend->addRow($dataMap->getTableName(), $row);
597 $object->_setProperty('uid', (integer) $uid);
598 if ((integer) $uid >= 1) {
599 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterInsertObject', array('object' => $object));
600 }
601 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
602 if ($frameworkConfiguration['persistence']['updateReferenceIndex'] === '1') {
603 $this->referenceIndex->updateRefIndexTable($dataMap->getTableName(), $uid);
604 }
605 $this->identityMap->registerObject($object, $uid);
606 }
607
608 /**
609 * Tests, if the given Value Object already exists in the storage backend and if so, it returns the uid.
610 *
611 * @param \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object The object to be tested
612 * @return mixed The matching uid if an object was found, else FALSE
613 */
614 protected function getUidOfAlreadyPersistedValueObject(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object) {
615 return $this->storageBackend->getUidOfAlreadyPersistedValueObject($object);
616 }
617
618 /**
619 * Inserts mm-relation into a relation table
620 *
621 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The related object
622 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
623 * @param string $propertyName The name of the parent object's property where the related objects are stored in
624 * @param integer $sortingPosition Defaults to NULL
625 * @return int The uid of the inserted row
626 */
627 protected function insertRelationInRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $propertyName, $sortingPosition = NULL) {
628 $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
629 $columnMap = $dataMap->getColumnMap($propertyName);
630 $row = array(
631 $columnMap->getParentKeyFieldName() => (integer) $parentObject->getUid(),
632 $columnMap->getChildKeyFieldName() => (integer) $object->getUid(),
633 $columnMap->getChildSortByFieldName() => !is_null($sortingPosition) ? (integer) $sortingPosition : 0
634 );
635 $relationTableName = $columnMap->getRelationTableName();
636 // FIXME Reenable support for tablenames
637 // $childTableName = $columnMap->getChildTableName();
638 // if (isset($childTableName)) {
639 // $row['tablenames'] = $childTableName;
640 // }
641 if ($columnMap->getRelationTablePageIdColumnName() !== NULL) {
642 $row[$columnMap->getRelationTablePageIdColumnName()] = $this->determineStoragePageIdForNewRecord();
643 }
644 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
645 if (count($relationTableMatchFields)) {
646 foreach ($relationTableMatchFields as $matchField => $matchValue) {
647 $row[$matchField] = $matchValue;
648 }
649 }
650 $relationTableInsertFields = $columnMap->getRelationTableInsertFields();
651 if (count($relationTableInsertFields)) {
652 foreach ($relationTableInsertFields as $insertField => $insertValue) {
653 $row[$insertField] = $insertValue;
654 }
655 }
656 $res = $this->storageBackend->addRow($relationTableName, $row, TRUE);
657 return $res;
658 }
659
660 /**
661 * Delete all mm-relations of a parent from a relation table
662 *
663 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
664 * @param string $parentPropertyName The name of the parent object's property where the related objects are stored in
665 * @return bool
666 */
667 protected function deleteAllRelationsFromRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName) {
668 $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
669 $columnMap = $dataMap->getColumnMap($parentPropertyName);
670 $relationTableName = $columnMap->getRelationTableName();
671 $relationMatchFields = array(
672 $columnMap->getParentKeyFieldName() => (integer) $parentObject->getUid()
673 );
674 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
675 if (is_array($relationTableMatchFields) && count($relationTableMatchFields) > 0) {
676 $relationMatchFields = array_merge($relationTableMatchFields, $relationMatchFields);
677 }
678 $res = $this->storageBackend->removeRow($relationTableName, $relationMatchFields, FALSE);
679 return $res;
680 }
681
682 /**
683 * Delete an mm-relation from a relation table
684 *
685 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $relatedObject The related object
686 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
687 * @param string $parentPropertyName The name of the parent object's property where the related objects are stored in
688 * @return bool
689 */
690 protected function deleteRelationFromRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $relatedObject, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName) {
691 $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
692 $columnMap = $dataMap->getColumnMap($parentPropertyName);
693 $relationTableName = $columnMap->getRelationTableName();
694 $res = $this->storageBackend->removeRow($relationTableName, array(
695 $columnMap->getParentKeyFieldName() => (integer) $parentObject->getUid(),
696 $columnMap->getChildKeyFieldName() => (integer) $relatedObject->getUid()
697 ), FALSE);
698 return $res;
699 }
700
701 /**
702 * Updates a given object in the storage
703 *
704 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be updated
705 * @param array $row Row to be stored
706 * @return bool
707 */
708 protected function updateObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, array $row) {
709 $dataMap = $this->dataMapper->getDataMap(get_class($object));
710 $this->addCommonFieldsToRow($object, $row);
711 $row['uid'] = $object->getUid();
712 if ($dataMap->getLanguageIdColumnName() !== NULL) {
713 $row[$dataMap->getLanguageIdColumnName()] = $object->_getProperty('_languageUid');
714 if ($object->_getProperty('_localizedUid') !== NULL) {
715 $row['uid'] = $object->_getProperty('_localizedUid');
716 }
717 }
718 $res = $this->storageBackend->updateRow($dataMap->getTableName(), $row);
719 if ($res === TRUE) {
720 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterUpdateObject', array('object' => $object));
721 }
722 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
723 if ($frameworkConfiguration['persistence']['updateReferenceIndex'] === '1') {
724 $this->referenceIndex->updateRefIndexTable($dataMap->getTableName(), $row['uid']);
725 }
726 return $res;
727 }
728
729 /**
730 * Adds common databse fields to a row
731 *
732 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
733 * @param array $row
734 * @return void
735 */
736 protected function addCommonFieldsToRow(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, array &$row) {
737 $dataMap = $this->dataMapper->getDataMap(get_class($object));
738 $this->addCommonDateFieldsToRow($object, $row);
739 if ($dataMap->getRecordTypeColumnName() !== NULL && $dataMap->getRecordType() !== NULL) {
740 $row[$dataMap->getRecordTypeColumnName()] = $dataMap->getRecordType();
741 }
742 if ($object->_isNew() && !isset($row['pid'])) {
743 $row['pid'] = $this->determineStoragePageIdForNewRecord($object);
744 }
745 }
746
747 /**
748 * Adjustes the common date fields of the given row to the current time
749 *
750 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
751 * @param array $row The row to be updated
752 * @return void
753 */
754 protected function addCommonDateFieldsToRow(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, array &$row) {
755 $dataMap = $this->dataMapper->getDataMap(get_class($object));
756 if ($object->_isNew() && $dataMap->getCreationDateColumnName() !== NULL) {
757 $row[$dataMap->getCreationDateColumnName()] = $GLOBALS['EXEC_TIME'];
758 }
759 if ($dataMap->getModificationDateColumnName() !== NULL) {
760 $row[$dataMap->getModificationDateColumnName()] = $GLOBALS['EXEC_TIME'];
761 }
762 }
763
764 /**
765 * Iterate over deleted aggregate root objects and process them
766 *
767 * @return void
768 */
769 protected function processDeletedObjects() {
770 foreach ($this->deletedObjects as $object) {
771 $this->removeObject($object);
772 $this->identityMap->unregisterObject($object);
773 }
774 $this->deletedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
775 }
776
777 /**
778 * Deletes an object
779 *
780 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be removed from the storage
781 * @param bool $markAsDeleted Wether to just flag the row deleted (default) or really delete it
782 * @return void
783 */
784 protected function removeObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, $markAsDeleted = TRUE) {
785 $dataMap = $this->dataMapper->getDataMap(get_class($object));
786 $tableName = $dataMap->getTableName();
787 if ($markAsDeleted === TRUE && $dataMap->getDeletedFlagColumnName() !== NULL) {
788 $deletedColumnName = $dataMap->getDeletedFlagColumnName();
789 $row = array(
790 'uid' => $object->getUid(),
791 $deletedColumnName => 1
792 );
793 $this->addCommonDateFieldsToRow($object, $row);
794 $res = $this->storageBackend->updateRow($tableName, $row);
795 } else {
796 $res = $this->storageBackend->removeRow($tableName, array('uid' => $object->getUid()));
797 }
798 if ($res === TRUE) {
799 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterRemoveObject', array('object' => $object));
800 }
801 $this->removeRelatedObjects($object);
802 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
803 if ($frameworkConfiguration['persistence']['updateReferenceIndex'] === '1') {
804 $this->referenceIndex->updateRefIndexTable($tableName, $object->getUid());
805 }
806 }
807
808 /**
809 * Remove related objects
810 *
811 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to scanned for related objects
812 * @return void
813 */
814 protected function removeRelatedObjects(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object) {
815 $className = get_class($object);
816 $dataMap = $this->dataMapper->getDataMap($className);
817 $classSchema = $this->reflectionService->getClassSchema($className);
818 $properties = $object->_getProperties();
819 foreach ($properties as $propertyName => $propertyValue) {
820 $columnMap = $dataMap->getColumnMap($propertyName);
821 $propertyMetaData = $classSchema->getProperty($propertyName);
822 if ($propertyMetaData['cascade'] === 'remove') {
823 if ($columnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY) {
824 foreach ($propertyValue as $containedObject) {
825 $this->removeObject($containedObject);
826 }
827 } elseif ($propertyValue instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
828 $this->removeObject($propertyValue);
829 }
830 }
831 }
832 }
833
834 /**
835 * Determine the storage page ID for a given NEW record
836 *
837 * This does the following:
838 * - If the domain object has an accessible property 'pid' (i.e. through a getPid() method), that is used to store the record.
839 * - If there is a TypoScript configuration "classes.CLASSNAME.newRecordStoragePid", that is used to store new records.
840 * - If there is no such TypoScript configuration, it uses the first value of The "storagePid" taken for reading records.
841 *
842 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
843 * @return int the storage Page ID where the object should be stored
844 */
845 protected function determineStoragePageIdForNewRecord(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object = NULL) {
846 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
847 if ($object !== NULL) {
848 if (\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($object, 'pid')) {
849 $pid = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($object, 'pid');
850 if (isset($pid)) {
851 return (integer) $pid;
852 }
853 }
854 $className = get_class($object);
855 if (isset($frameworkConfiguration['persistence']['classes'][$className]) && !empty($frameworkConfiguration['persistence']['classes'][$className]['newRecordStoragePid'])) {
856 return (integer) $frameworkConfiguration['persistence']['classes'][$className]['newRecordStoragePid'];
857 }
858 }
859 $storagePidList = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $frameworkConfiguration['persistence']['storagePid']);
860 return (integer) $storagePidList[0];
861 }
862
863 /**
864 * Returns a plain value, i.e. objects are flattened out if possible.
865 *
866 * @param mixed $input
867 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap $columnMap
868 * @return mixed
869 */
870 protected function getPlainValue($input, $columnMap = NULL) {
871 if ($input instanceof \DateTime) {
872 if (!is_null($columnMap) && !is_null($columnMap->getDateTimeStorageFormat())) {
873 if ($columnMap->getDateTimeStorageFormat() == 'datetime') {
874 return $input->format('Y-m-d H:i:s');
875 }
876 if ($columnMap->getDateTimeStorageFormat() == 'date') {
877 return $input->format('Y-m-d');
878 }
879 } else {
880 return $input->format('U');
881 }
882 } elseif ($input instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
883 return $input->getUid();
884 } elseif (is_bool($input)) {
885 return $input === TRUE ? 1 : 0;
886 } else {
887 return $input;
888 }
889 }
890 }
891
892 ?>