2 namespace TYPO3\CMS\Extbase\Persistence\Generic
;
5 * This file is part of the TYPO3 CMS project.
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
;
18 use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
;
19 use TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface
;
22 * A persistence backend. This backend maps objects to the relational model of the storage backend.
23 * It persists all added, removed and changed objects.
25 class Backend
implements \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
, \TYPO3\CMS\Core\SingletonInterface
28 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
33 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
35 protected $persistenceManager;
38 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
40 protected $aggregateRootObjects;
43 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
45 protected $deletedEntities;
48 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
50 protected $changedEntities;
53 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
55 protected $visitedDuringPersistence;
58 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
60 protected $reflectionService;
63 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
65 protected $qomFactory;
68 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface
70 protected $storageBackend;
73 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
75 protected $dataMapper;
78 * The TYPO3 reference index object
80 * @var \TYPO3\CMS\Core\Database\ReferenceIndex
82 protected $referenceIndex;
85 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
87 protected $configurationManager;
90 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
92 protected $signalSlotDispatcher;
95 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Session $session
97 public function injectSession(\TYPO3\CMS\Extbase\Persistence\Generic\Session
$session)
99 $this->session
= $session;
103 * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService
105 public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService
$reflectionService)
107 $this->reflectionService
= $reflectionService;
111 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory $qomFactory
113 public function injectQomFactory(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
$qomFactory)
115 $this->qomFactory
= $qomFactory;
119 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface $storageBackend
121 public function injectStorageBackend(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface
$storageBackend)
123 $this->storageBackend
= $storageBackend;
127 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper
129 public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
$dataMapper)
131 $this->dataMapper
= $dataMapper;
135 * @param \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher
137 public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher
$signalSlotDispatcher)
139 $this->signalSlotDispatcher
= $signalSlotDispatcher;
143 * Constructs the backend
145 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
147 public function __construct(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
$configurationManager)
149 $this->configurationManager
= $configurationManager;
150 $this->referenceIndex
= \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance(\TYPO3\CMS\Core\Database\ReferenceIndex
::class);
151 $this->referenceIndex
->enableRuntimeCache();
152 $this->aggregateRootObjects
= new \TYPO3\CMS\Extbase\Persistence\
ObjectStorage();
153 $this->deletedEntities
= new \TYPO3\CMS\Extbase\Persistence\
ObjectStorage();
154 $this->changedEntities
= new \TYPO3\CMS\Extbase\Persistence\
ObjectStorage();
158 * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager
160 public function setPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
$persistenceManager)
162 $this->persistenceManager
= $persistenceManager;
166 * Returns the repository session
168 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Session
170 public function getSession()
172 return $this->session
;
176 * Returns the Data Mapper
178 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
180 public function getDataMapper()
182 return $this->dataMapper
;
186 * Returns the current QOM factory
188 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
190 public function getQomFactory()
192 return $this->qomFactory
;
196 * Returns the reflection service
198 * @return \TYPO3\CMS\Extbase\Reflection\ReflectionService
200 public function getReflectionService()
202 return $this->reflectionService
;
206 * Returns the number of records matching the query.
208 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
212 public function getObjectCountByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface
$query)
214 return $this->storageBackend
->getObjectCountByQuery($query);
218 * Returns the object data matching the $query.
220 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
224 public function getObjectDataByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface
$query)
226 $query = $this->emitBeforeGettingObjectDataSignal($query);
227 $result = $this->storageBackend
->getObjectDataByQuery($query);
228 $result = $this->emitAfterGettingObjectDataSignal($query, $result);
233 * Emits a signal before object data is fetched
235 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
236 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface Modified query
238 protected function emitBeforeGettingObjectDataSignal(\TYPO3\CMS\Extbase\Persistence\QueryInterface
$query)
240 $signalArguments = $this->signalSlotDispatcher
->dispatch(__CLASS__
, 'beforeGettingObjectData', [$query]);
241 return $signalArguments[0];
245 * Emits a signal after object data is fetched
247 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
248 * @param array $result
249 * @return array Modified result
251 protected function emitAfterGettingObjectDataSignal(\TYPO3\CMS\Extbase\Persistence\QueryInterface
$query, array $result)
253 $signalArguments = $this->signalSlotDispatcher
->dispatch(__CLASS__
, 'afterGettingObjectData', [$query, $result]);
254 return $signalArguments[1];
258 * Returns the (internal) identifier for the object, if it is known to the
259 * backend. Otherwise NULL is returned.
261 * @param object $object
262 * @return string|NULL The identifier for the object if it is known, or NULL
264 public function getIdentifierByObject($object)
266 if ($object instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy
) {
267 $object = $object->_loadRealInstance();
268 if (!is_object($object)) {
272 return $this->session
->getIdentifierByObject($object);
276 * Returns the object with the (internal) identifier, if it is known to the
277 * backend. Otherwise NULL is returned.
279 * @param string $identifier
280 * @param string $className
281 * @return object|NULL The object for the identifier if it is known, or NULL
283 public function getObjectByIdentifier($identifier, $className)
285 if ($this->session
->hasIdentifier($identifier, $className)) {
286 return $this->session
->getObjectByIdentifier($identifier, $className);
288 $query = $this->persistenceManager
->createQueryForType($className);
289 $query->getQuerySettings()->setRespectStoragePage(false);
290 $query->getQuerySettings()->setRespectSysLanguage(false);
291 return $query->matching($query->equals('uid', $identifier))->execute()->getFirst();
295 * Checks if the given object has ever been persisted.
297 * @param object $object The object to check
298 * @return bool TRUE if the object is new, FALSE if the object exists in the repository
300 public function isNewObject($object)
302 return $this->getIdentifierByObject($object) === null;
306 * Sets the aggregate root objects
308 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $objects
310 public function setAggregateRootObjects(\TYPO3\CMS\Extbase\Persistence\ObjectStorage
$objects)
312 $this->aggregateRootObjects
= $objects;
316 * Sets the changed objects
318 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $entities
320 public function setChangedEntities(\TYPO3\CMS\Extbase\Persistence\ObjectStorage
$entities)
322 $this->changedEntities
= $entities;
326 * Sets the deleted objects
328 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $entities
330 public function setDeletedEntities(\TYPO3\CMS\Extbase\Persistence\ObjectStorage
$entities)
332 $this->deletedEntities
= $entities;
336 * Commits the current persistence session.
338 public function commit()
340 $this->persistObjects();
341 $this->processDeletedObjects();
345 * Traverse and persist all aggregate roots and their object graph.
347 protected function persistObjects()
349 $this->visitedDuringPersistence
= new \TYPO3\CMS\Extbase\Persistence\
ObjectStorage();
350 foreach ($this->aggregateRootObjects
as $object) {
351 /** @var DomainObjectInterface $object */
352 if ($object->_isNew()) {
353 $this->insertObject($object);
355 $this->persistObject($object, null);
357 foreach ($this->changedEntities
as $object) {
358 $this->persistObject($object, null);
363 * Persists the given object.
365 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be inserted
367 protected function persistObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object)
369 if (isset($this->visitedDuringPersistence
[$object])) {
374 $dataMap = $this->dataMapper
->getDataMap(get_class($object));
375 $properties = $object->_getProperties();
376 foreach ($properties as $propertyName => $propertyValue) {
377 if (!$dataMap->isPersistableProperty($propertyName) ||
$this->propertyValueIsLazyLoaded($propertyValue)) {
380 $columnMap = $dataMap->getColumnMap($propertyName);
381 if ($propertyValue instanceof \TYPO3\CMS\Extbase\Persistence\ObjectStorage
) {
382 $cleanProperty = $object->_getCleanProperty($propertyName);
383 // objectstorage needs to be persisted if the object is new, the objectstorge is dirty, meaning it has
384 // been changed after initial build, or an empty objectstorge is present and the cleanstate objectstorage
385 // has childelements, meaning all elements should been removed from the objectstorage
386 if ($object->_isNew() ||
$propertyValue->_isDirty() ||
($propertyValue->count() === 0 && $cleanProperty && $cleanProperty->count() > 0)) {
387 $this->persistObjectStorage($propertyValue, $object, $propertyName, $row);
388 $propertyValue->_memorizeCleanState();
390 foreach ($propertyValue as $containedObject) {
391 if ($containedObject instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
) {
392 $queue[] = $containedObject;
395 } elseif ($propertyValue instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
396 && $object instanceof ObjectMonitoringInterface
) {
397 if ($object->_isDirty($propertyName)) {
398 if ($propertyValue->_isNew()) {
399 $this->insertObject($propertyValue, $object, $propertyName);
401 // Check explicitly for NULL, as getPlainValue would convert this to 'NULL'
402 $row[$columnMap->getColumnName()] = $propertyValue !== null
403 ?
$this->dataMapper
->getPlainValue($propertyValue)
406 $queue[] = $propertyValue;
407 } elseif ($object->_isNew() ||
$object->_isDirty($propertyName)) {
408 $row[$columnMap->getColumnName()] = $this->dataMapper
->getPlainValue($propertyValue, $columnMap);
412 $this->updateObject($object, $row);
413 $object->_memorizeCleanState();
415 $this->visitedDuringPersistence
[$object] = $object->getUid();
416 foreach ($queue as $queuedObject) {
417 $this->persistObject($queuedObject);
419 $this->emitAfterPersistObjectSignal($object);
423 * Checks, if the property value is lazy loaded and was not initialized
425 * @param mixed $propertyValue The property value
428 protected function propertyValueIsLazyLoaded($propertyValue)
430 if ($propertyValue instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy
) {
433 if ($propertyValue instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage
) {
434 if ($propertyValue->isInitialized() === false) {
442 * Persists an object storage. Objects of a 1:n or m:n relation are queued and processed with the parent object. A 1:1 relation
443 * gets persisted immediately. Objects which were removed from the property were detached from the parent object. They will not be
444 * deleted by default. You have to annotate the property with "@cascade remove" if you want them to be deleted as well.
446 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $objectStorage The object storage to be persisted.
447 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object. One of the properties holds the object storage.
448 * @param string $propertyName The name of the property holding the object storage.
449 * @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.
451 protected function persistObjectStorage(\TYPO3\CMS\Extbase\Persistence\ObjectStorage
$objectStorage, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$parentObject, $propertyName, array &$row)
453 $className = get_class($parentObject);
454 $columnMap = $this->dataMapper
->getDataMap($className)->getColumnMap($propertyName);
455 $propertyMetaData = $this->reflectionService
->getClassSchema($className)->getProperty($propertyName);
456 foreach ($this->getRemovedChildObjects($parentObject, $propertyName) as $removedObject) {
457 $this->detachObjectFromParentObject($removedObject, $parentObject, $propertyName);
458 if ($columnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
::RELATION_HAS_MANY
&& $propertyMetaData['annotations']['cascade'] === 'remove') {
459 $this->removeEntity($removedObject);
464 $sortingPosition = 1;
465 $updateSortingOfFollowing = false;
467 foreach ($objectStorage as $object) {
468 /** @var DomainObjectInterface $object */
469 if (empty($currentUids)) {
470 $sortingPosition = 1;
474 $cleanProperty = $parentObject->_getCleanProperty($propertyName);
475 if ($object->_isNew()) {
476 $this->insertObject($object);
477 $this->attachObjectToParentObject($object, $parentObject, $propertyName, $sortingPosition);
478 // if a new object is inserted, all objects after this need to have their sorting updated
479 $updateSortingOfFollowing = true;
480 } elseif ($cleanProperty === null ||
$cleanProperty->getPosition($object) === null) {
481 // if parent object is new then it doesn't have cleanProperty yet; before attaching object it's clean position is null
482 $this->attachObjectToParentObject($object, $parentObject, $propertyName, $sortingPosition);
483 // if a relation is dirty (speaking the same object is removed and added again at a different position), all objects after this needs to be updated the sorting
484 $updateSortingOfFollowing = true;
485 } elseif ($objectStorage->isRelationDirty($object) ||
$cleanProperty->getPosition($object) !== $objectStorage->getPosition($object)) {
486 $this->updateRelationOfObjectToParentObject($object, $parentObject, $propertyName, $sortingPosition);
487 $updateSortingOfFollowing = true;
488 } elseif ($updateSortingOfFollowing) {
489 if ($sortingPosition > $objectStorage->getPosition($object)) {
490 $this->updateRelationOfObjectToParentObject($object, $parentObject, $propertyName, $sortingPosition);
492 $sortingPosition = $objectStorage->getPosition($object);
495 $currentUids[] = $object->getUid();
498 if ($columnMap->getParentKeyFieldName() === null) {
499 $row[$columnMap->getColumnName()] = implode(',', $currentUids);
501 $row[$columnMap->getColumnName()] = $this->dataMapper
->countRelated($parentObject, $propertyName);
506 * Returns the removed objects determined by a comparison of the clean property value
507 * with the actual property value.
509 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object
510 * @param string $propertyName
511 * @return array An array of removed objects
513 protected function getRemovedChildObjects(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object, $propertyName)
515 $removedObjects = [];
516 $cleanPropertyValue = $object->_getCleanProperty($propertyName);
517 if (is_array($cleanPropertyValue) ||
$cleanPropertyValue instanceof \Iterator
) {
518 $propertyValue = $object->_getProperty($propertyName);
519 foreach ($cleanPropertyValue as $containedObject) {
520 if (!$propertyValue->contains($containedObject)) {
521 $removedObjects[] = $containedObject;
525 return $removedObjects;
529 * Updates the fields defining the relation between the object and the parent object.
531 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
532 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject
533 * @param string $parentPropertyName
534 * @param int $sortingPosition
536 protected function attachObjectToParentObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$parentObject, $parentPropertyName, $sortingPosition = 0)
538 $parentDataMap = $this->dataMapper
->getDataMap(get_class($parentObject));
539 $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
540 if ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
::RELATION_HAS_MANY
) {
541 $this->attachObjectToParentObjectRelationHasMany($object, $parentObject, $parentPropertyName, $sortingPosition);
542 } elseif ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
::RELATION_HAS_AND_BELONGS_TO_MANY
) {
543 $this->insertRelationInRelationtable($object, $parentObject, $parentPropertyName, $sortingPosition);
548 * Updates the fields defining the relation between the object and the parent object.
550 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
551 * @param \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $parentObject
552 * @param string $parentPropertyName
553 * @param int $sortingPosition
555 protected function updateRelationOfObjectToParentObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object, \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
$parentObject, $parentPropertyName, $sortingPosition = 0)
557 $parentDataMap = $this->dataMapper
->getDataMap(get_class($parentObject));
558 $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
559 if ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
::RELATION_HAS_MANY
) {
560 $this->attachObjectToParentObjectRelationHasMany($object, $parentObject, $parentPropertyName, $sortingPosition);
561 } elseif ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
::RELATION_HAS_AND_BELONGS_TO_MANY
) {
562 $this->updateRelationInRelationTable($object, $parentObject, $parentPropertyName, $sortingPosition);
567 * Updates fields defining the relation between the object and the parent object in relation has-many.
569 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
570 * @param \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $parentObject
571 * @param string $parentPropertyName
572 * @param int $sortingPosition
573 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalRelationTypeException
575 protected function attachObjectToParentObjectRelationHasMany(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object, \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
$parentObject, $parentPropertyName, $sortingPosition = 0)
577 $parentDataMap = $this->dataMapper
->getDataMap(get_class($parentObject));
578 $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
579 if ($parentColumnMap->getTypeOfRelation() !== \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
::RELATION_HAS_MANY
) {
580 throw new \TYPO3\CMS\Extbase\Persistence\Exception\
IllegalRelationTypeException(
581 'Parent column relation type is ' . $parentColumnMap->getTypeOfRelation() .
582 ' but should be ' . \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
::RELATION_HAS_MANY
,
587 $parentKeyFieldName = $parentColumnMap->getParentKeyFieldName();
588 if ($parentKeyFieldName !== null) {
589 $row[$parentKeyFieldName] = $parentObject->getUid();
590 $parentTableFieldName = $parentColumnMap->getParentTableFieldName();
591 if ($parentTableFieldName !== null) {
592 $row[$parentTableFieldName] = $parentDataMap->getTableName();
594 $relationTableMatchFields = $parentColumnMap->getRelationTableMatchFields();
595 if (is_array($relationTableMatchFields)) {
596 $row = array_merge($relationTableMatchFields, $row);
599 $childSortByFieldName = $parentColumnMap->getChildSortByFieldName();
600 if (!empty($childSortByFieldName)) {
601 $row[$childSortByFieldName] = $sortingPosition;
604 $this->updateObject($object, $row);
609 * Updates the fields defining the relation between the object and the parent object.
611 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
612 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject
613 * @param string $parentPropertyName
615 protected function detachObjectFromParentObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$parentObject, $parentPropertyName)
617 $parentDataMap = $this->dataMapper
->getDataMap(get_class($parentObject));
618 $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
619 if ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
::RELATION_HAS_MANY
) {
621 $parentKeyFieldName = $parentColumnMap->getParentKeyFieldName();
622 if ($parentKeyFieldName !== null) {
623 $row[$parentKeyFieldName] = 0;
624 $parentTableFieldName = $parentColumnMap->getParentTableFieldName();
625 if ($parentTableFieldName !== null) {
626 $row[$parentTableFieldName] = '';
628 $relationTableMatchFields = $parentColumnMap->getRelationTableMatchFields();
629 if (is_array($relationTableMatchFields) && !empty($relationTableMatchFields)) {
630 $row = array_merge(array_fill_keys(array_keys($relationTableMatchFields), ''), $row);
633 $childSortByFieldName = $parentColumnMap->getChildSortByFieldName();
634 if (!empty($childSortByFieldName)) {
635 $row[$childSortByFieldName] = 0;
638 $this->updateObject($object, $row);
640 } elseif ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
::RELATION_HAS_AND_BELONGS_TO_MANY
) {
641 $this->deleteRelationFromRelationtable($object, $parentObject, $parentPropertyName);
646 * Inserts an object in the storage backend
648 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be insterted in the storage
649 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parentobject.
650 * @param string $parentPropertyName
652 protected function insertObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$parentObject = null, $parentPropertyName = '')
654 if ($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject
) {
655 $result = $this->getUidOfAlreadyPersistedValueObject($object);
656 if ($result !== false) {
657 $object->_setProperty('uid', (int)$result);
661 $dataMap = $this->dataMapper
->getDataMap(get_class($object));
663 $properties = $object->_getProperties();
664 foreach ($properties as $propertyName => $propertyValue) {
665 if (!$dataMap->isPersistableProperty($propertyName) ||
$this->propertyValueIsLazyLoaded($propertyValue)) {
668 $columnMap = $dataMap->getColumnMap($propertyName);
669 if ($columnMap->getTypeOfRelation() === ColumnMap
::RELATION_HAS_ONE
) {
670 $row[$columnMap->getColumnName()] = 0;
671 } elseif ($columnMap->getTypeOfRelation() !== ColumnMap
::RELATION_NONE
) {
672 if ($columnMap->getParentKeyFieldName() === null) {
674 $row[$columnMap->getColumnName()] = '';
677 $row[$columnMap->getColumnName()] = 0;
679 } elseif ($propertyValue !== null) {
680 $row[$columnMap->getColumnName()] = $this->dataMapper
->getPlainValue($propertyValue, $columnMap);
683 $this->addCommonFieldsToRow($object, $row);
684 if ($dataMap->getLanguageIdColumnName() !== null && $object->_getProperty('_languageUid') === null) {
685 $row[$dataMap->getLanguageIdColumnName()] = 0;
686 $object->_setProperty('_languageUid', 0);
688 if ($dataMap->getTranslationOriginColumnName() !== null) {
689 $row[$dataMap->getTranslationOriginColumnName()] = 0;
691 if ($dataMap->getTranslationOriginDiffSourceName() !== null) {
692 $row[$dataMap->getTranslationOriginDiffSourceName()] = '';
694 if ($parentObject !== null && $parentPropertyName) {
695 $parentColumnDataMap = $this->dataMapper
->getDataMap(get_class($parentObject))->getColumnMap($parentPropertyName);
696 $relationTableMatchFields = $parentColumnDataMap->getRelationTableMatchFields();
697 if (is_array($relationTableMatchFields)) {
698 $row = array_merge($relationTableMatchFields, $row);
700 if ($parentColumnDataMap->getParentKeyFieldName() !== null) {
701 $row[$parentColumnDataMap->getParentKeyFieldName()] = (int)$parentObject->getUid();
704 $uid = $this->storageBackend
->addRow($dataMap->getTableName(), $row);
705 $object->_setProperty('uid', (int)$uid);
706 $object->setPid((int)$row['pid']);
707 if ((int)$uid >= 1) {
708 $this->emitAfterInsertObjectSignal($object);
710 $frameworkConfiguration = $this->configurationManager
->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
::CONFIGURATION_TYPE_FRAMEWORK
);
711 if ($frameworkConfiguration['persistence']['updateReferenceIndex'] === '1') {
712 $this->referenceIndex
->updateRefIndexTable($dataMap->getTableName(), $uid);
714 $this->session
->registerObject($object, $uid);
715 if ((int)$uid >= 1) {
716 $this->emitEndInsertObjectSignal($object);
721 * Emits a signal after an object was added to the storage
723 * @param DomainObjectInterface $object
725 protected function emitAfterInsertObjectSignal(DomainObjectInterface
$object)
727 $this->signalSlotDispatcher
->dispatch(__CLASS__
, 'afterInsertObject', [$object]);
731 * Emits a signal after an object was registered in persistence session
732 * This signal replaces the afterInsertObject signal which is now deprecated
734 * @param DomainObjectInterface $object
736 protected function emitEndInsertObjectSignal(DomainObjectInterface
$object)
738 $this->signalSlotDispatcher
->dispatch(__CLASS__
, 'endInsertObject', [$object]);
742 * Tests, if the given Value Object already exists in the storage backend and if so, it returns the uid.
744 * @param \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object The object to be tested
745 * @return mixed The matching uid if an object was found, else FALSE
747 protected function getUidOfAlreadyPersistedValueObject(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject
$object)
749 return $this->storageBackend
->getUidOfAlreadyPersistedValueObject($object);
753 * Inserts mm-relation into a relation table
755 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The related object
756 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
757 * @param string $propertyName The name of the parent object's property where the related objects are stored in
758 * @param int $sortingPosition Defaults to NULL
759 * @return int The uid of the inserted row
761 protected function insertRelationInRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$parentObject, $propertyName, $sortingPosition = null)
763 $dataMap = $this->dataMapper
->getDataMap(get_class($parentObject));
764 $columnMap = $dataMap->getColumnMap($propertyName);
765 $parentUid = $parentObject->getUid();
766 if (!empty($parentObject->_getProperty('_localizedUid'))) {
767 $parentUid = $parentObject->_getProperty('_localizedUid');
770 $columnMap->getParentKeyFieldName() => (int)$parentUid,
771 $columnMap->getChildKeyFieldName() => (int)$object->getUid(),
772 $columnMap->getChildSortByFieldName() => !is_null($sortingPosition) ?
(int)$sortingPosition : 0
774 $relationTableName = $columnMap->getRelationTableName();
775 if ($columnMap->getRelationTablePageIdColumnName() !== null) {
776 $row[$columnMap->getRelationTablePageIdColumnName()] = $this->determineStoragePageIdForNewRecord();
778 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
779 if (is_array($relationTableMatchFields)) {
780 $row = array_merge($relationTableMatchFields, $row);
782 $relationTableInsertFields = $columnMap->getRelationTableInsertFields();
783 if (is_array($relationTableInsertFields)) {
784 $row = array_merge($relationTableInsertFields, $row);
786 $res = $this->storageBackend
->addRow($relationTableName, $row, true);
791 * Updates mm-relation in a relation table
793 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The related object
794 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
795 * @param string $propertyName The name of the parent object's property where the related objects are stored in
796 * @param int $sortingPosition Defaults to NULL
797 * @return bool TRUE if update was successfully
799 protected function updateRelationInRelationTable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$parentObject, $propertyName, $sortingPosition = 0)
801 $dataMap = $this->dataMapper
->getDataMap(get_class($parentObject));
802 $columnMap = $dataMap->getColumnMap($propertyName);
804 $columnMap->getParentKeyFieldName() => (int)$parentObject->getUid(),
805 $columnMap->getChildKeyFieldName() => (int)$object->getUid(),
806 $columnMap->getChildSortByFieldName() => (int)$sortingPosition
808 $relationTableName = $columnMap->getRelationTableName();
809 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
810 if (is_array($relationTableMatchFields)) {
811 $row = array_merge($relationTableMatchFields, $row);
813 $res = $this->storageBackend
->updateRelationTableRow(
821 * Delete all mm-relations of a parent from a relation table
823 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
824 * @param string $parentPropertyName The name of the parent object's property where the related objects are stored in
825 * @return bool TRUE if delete was successfully
827 protected function deleteAllRelationsFromRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$parentObject, $parentPropertyName)
829 $dataMap = $this->dataMapper
->getDataMap(get_class($parentObject));
830 $columnMap = $dataMap->getColumnMap($parentPropertyName);
831 $relationTableName = $columnMap->getRelationTableName();
832 $relationMatchFields = [
833 $columnMap->getParentKeyFieldName() => (int)$parentObject->getUid()
835 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
836 if (is_array($relationTableMatchFields)) {
837 $relationMatchFields = array_merge($relationTableMatchFields, $relationMatchFields);
839 $res = $this->storageBackend
->removeRow($relationTableName, $relationMatchFields, false);
844 * Delete an mm-relation from a relation table
846 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $relatedObject The related object
847 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
848 * @param string $parentPropertyName The name of the parent object's property where the related objects are stored in
851 protected function deleteRelationFromRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$relatedObject, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$parentObject, $parentPropertyName)
853 $dataMap = $this->dataMapper
->getDataMap(get_class($parentObject));
854 $columnMap = $dataMap->getColumnMap($parentPropertyName);
855 $relationTableName = $columnMap->getRelationTableName();
856 $relationMatchFields = [
857 $columnMap->getParentKeyFieldName() => (int)$parentObject->getUid(),
858 $columnMap->getChildKeyFieldName() => (int)$relatedObject->getUid()
860 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
861 if (is_array($relationTableMatchFields)) {
862 $relationMatchFields = array_merge($relationTableMatchFields, $relationMatchFields);
864 $res = $this->storageBackend
->removeRow($relationTableName, $relationMatchFields, false);
869 * Fetches maximal value currently used for sorting field in parent table
871 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
872 * @param string $parentPropertyName The name of the parent object's property where the related objects are stored in
873 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalRelationTypeException
874 * @return mixed the max value
876 protected function fetchMaxSortingFromParentTable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$parentObject, $parentPropertyName)
878 $parentDataMap = $this->dataMapper
->getDataMap(get_class($parentObject));
879 $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
880 if ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
::RELATION_HAS_MANY
) {
881 $tableName = $parentColumnMap->getChildTableName();
882 $sortByFieldName = $parentColumnMap->getChildSortByFieldName();
884 if (empty($sortByFieldName)) {
888 $parentKeyFieldName = $parentColumnMap->getParentKeyFieldName();
889 if ($parentKeyFieldName !== null) {
890 $matchFields[$parentKeyFieldName] = $parentObject->getUid();
891 $parentTableFieldName = $parentColumnMap->getParentTableFieldName();
892 if ($parentTableFieldName !== null) {
893 $matchFields[$parentTableFieldName] = $parentDataMap->getTableName();
897 if (empty($matchFields)) {
900 } elseif ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
::RELATION_HAS_AND_BELONGS_TO_MANY
) {
901 $tableName = $parentColumnMap->getRelationTableName();
902 $sortByFieldName = $parentColumnMap->getChildSortByFieldName();
905 $parentColumnMap->getParentKeyFieldName() => (int)$parentObject->getUid()
908 $relationTableMatchFields = $parentColumnMap->getRelationTableMatchFields();
909 if (is_array($relationTableMatchFields)) {
910 $matchFields = array_merge($relationTableMatchFields, $matchFields);
913 throw new \TYPO3\CMS\Extbase\Persistence\Exception\
IllegalRelationTypeException('Unexpected parent column relation type: ' . $parentColumnMap->getTypeOfRelation(), 1345368106);
916 $result = $this->storageBackend
->getMaxValueFromTable(
925 * Updates a given object in the storage
927 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be updated
928 * @param array $row Row to be stored
931 protected function updateObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object, array $row)
933 $dataMap = $this->dataMapper
->getDataMap(get_class($object));
934 $this->addCommonFieldsToRow($object, $row);
935 $row['uid'] = $object->getUid();
936 if ($dataMap->getLanguageIdColumnName() !== null) {
937 $row[$dataMap->getLanguageIdColumnName()] = (int)$object->_getProperty('_languageUid');
938 if (!empty($object->_getProperty('_localizedUid'))) {
939 $row['uid'] = $object->_getProperty('_localizedUid');
942 $res = $this->storageBackend
->updateRow($dataMap->getTableName(), $row);
944 $this->emitAfterUpdateObjectSignal($object);
946 $frameworkConfiguration = $this->configurationManager
->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
::CONFIGURATION_TYPE_FRAMEWORK
);
947 if ($frameworkConfiguration['persistence']['updateReferenceIndex'] === '1') {
948 $this->referenceIndex
->updateRefIndexTable($dataMap->getTableName(), $row['uid']);
954 * Emits a signal after an object was updated in storage
956 * @param DomainObjectInterface $object
958 protected function emitAfterUpdateObjectSignal(DomainObjectInterface
$object)
960 $this->signalSlotDispatcher
->dispatch(__CLASS__
, 'afterUpdateObject', [$object]);
964 * Emits a signal after an object was persisted
966 * @param DomainObjectInterface $object
968 protected function emitAfterPersistObjectSignal(DomainObjectInterface
$object)
970 $this->signalSlotDispatcher
->dispatch(__CLASS__
, 'afterPersistObject', [$object]);
974 * Adds common databse fields to a row
976 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
979 protected function addCommonFieldsToRow(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object, array &$row)
981 $dataMap = $this->dataMapper
->getDataMap(get_class($object));
982 $this->addCommonDateFieldsToRow($object, $row);
983 if ($dataMap->getRecordTypeColumnName() !== null && $dataMap->getRecordType() !== null) {
984 $row[$dataMap->getRecordTypeColumnName()] = $dataMap->getRecordType();
986 if ($object->_isNew() && !isset($row['pid'])) {
987 $row['pid'] = $this->determineStoragePageIdForNewRecord($object);
992 * Adjustes the common date fields of the given row to the current time
994 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
995 * @param array &$row The row to be updated
997 protected function addCommonDateFieldsToRow(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object, array &$row)
999 $dataMap = $this->dataMapper
->getDataMap(get_class($object));
1000 if ($object->_isNew() && $dataMap->getCreationDateColumnName() !== null) {
1001 $row[$dataMap->getCreationDateColumnName()] = $GLOBALS['EXEC_TIME'];
1003 if ($dataMap->getModificationDateColumnName() !== null) {
1004 $row[$dataMap->getModificationDateColumnName()] = $GLOBALS['EXEC_TIME'];
1009 * Iterate over deleted aggregate root objects and process them
1011 protected function processDeletedObjects()
1013 foreach ($this->deletedEntities
as $entity) {
1014 if ($this->session
->hasObject($entity)) {
1015 $this->removeEntity($entity);
1016 $this->session
->unregisterReconstitutedEntity($entity);
1017 $this->session
->unregisterObject($entity);
1020 $this->deletedEntities
= new \TYPO3\CMS\Extbase\Persistence\
ObjectStorage();
1026 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be removed from the storage
1027 * @param bool $markAsDeleted Whether to just flag the row deleted (default) or really delete it
1029 protected function removeEntity(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object, $markAsDeleted = true)
1031 $dataMap = $this->dataMapper
->getDataMap(get_class($object));
1032 $tableName = $dataMap->getTableName();
1033 if ($markAsDeleted === true && $dataMap->getDeletedFlagColumnName() !== null) {
1034 $deletedColumnName = $dataMap->getDeletedFlagColumnName();
1036 'uid' => $object->getUid(),
1037 $deletedColumnName => 1
1039 $this->addCommonDateFieldsToRow($object, $row);
1040 $res = $this->storageBackend
->updateRow($tableName, $row);
1042 $res = $this->storageBackend
->removeRow($tableName, ['uid' => $object->getUid()]);
1044 if ($res === true) {
1045 $this->emitAfterRemoveObjectSignal($object);
1047 $this->removeRelatedObjects($object);
1048 $frameworkConfiguration = $this->configurationManager
->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
::CONFIGURATION_TYPE_FRAMEWORK
);
1049 if ($frameworkConfiguration['persistence']['updateReferenceIndex'] === '1') {
1050 $this->referenceIndex
->updateRefIndexTable($tableName, $object->getUid());
1055 * Emits a signal after an object was removed from storage
1057 * @param DomainObjectInterface $object
1059 protected function emitAfterRemoveObjectSignal(DomainObjectInterface
$object)
1061 $this->signalSlotDispatcher
->dispatch(__CLASS__
, 'afterRemoveObject', [$object]);
1065 * Remove related objects
1067 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to scanned for related objects
1069 protected function removeRelatedObjects(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object)
1071 $className = get_class($object);
1072 $dataMap = $this->dataMapper
->getDataMap($className);
1073 $classSchema = $this->reflectionService
->getClassSchema($className);
1074 $properties = $object->_getProperties();
1075 foreach ($properties as $propertyName => $propertyValue) {
1076 $columnMap = $dataMap->getColumnMap($propertyName);
1077 if ($columnMap === null) {
1080 $propertyMetaData = $classSchema->getProperty($propertyName);
1081 if ($propertyMetaData['annotations']['cascade'] === 'remove') {
1082 if ($columnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
::RELATION_HAS_MANY
) {
1083 foreach ($propertyValue as $containedObject) {
1084 $this->removeEntity($containedObject);
1086 } elseif ($propertyValue instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
) {
1087 $this->removeEntity($propertyValue);
1089 } elseif ($dataMap->getDeletedFlagColumnName() === null
1090 && $columnMap->getTypeOfRelation() === ColumnMap
::RELATION_HAS_AND_BELONGS_TO_MANY
1092 $this->deleteAllRelationsFromRelationtable($object, $propertyName);
1098 * Determine the storage page ID for a given NEW record
1100 * This does the following:
1101 * - If the domain object has an accessible property 'pid' (i.e. through a getPid() method), that is used to store the record.
1102 * - If there is a TypoScript configuration "classes.CLASSNAME.newRecordStoragePid", that is used to store new records.
1103 * - If there is no such TypoScript configuration, it uses the first value of The "storagePid" taken for reading records.
1105 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
1106 * @return int the storage Page ID where the object should be stored
1108 protected function determineStoragePageIdForNewRecord(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
$object = null)
1110 $frameworkConfiguration = $this->configurationManager
->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
::CONFIGURATION_TYPE_FRAMEWORK
);
1111 if ($object !== null) {
1112 if (\TYPO3\CMS\Extbase\Reflection\ObjectAccess
::isPropertyGettable($object, 'pid')) {
1113 $pid = \TYPO3\CMS\Extbase\Reflection\ObjectAccess
::getProperty($object, 'pid');
1118 $className = get_class($object);
1119 if (isset($frameworkConfiguration['persistence']['classes'][$className]) && !empty($frameworkConfiguration['persistence']['classes'][$className]['newRecordStoragePid'])) {
1120 return (int)$frameworkConfiguration['persistence']['classes'][$className]['newRecordStoragePid'];
1123 $storagePidList = \TYPO3\CMS\Core\Utility\GeneralUtility
::intExplode(',', $frameworkConfiguration['persistence']['storagePid']);
1124 return (int)$storagePidList[0];