[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / Backend.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
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.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
18 use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap;
19 use TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface;
20
21 /**
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.
24 */
25 class Backend implements \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface, \TYPO3\CMS\Core\SingletonInterface
26 {
27 /**
28 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
29 */
30 protected $session;
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
34 */
35 protected $persistenceManager;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
39 */
40 protected $aggregateRootObjects;
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
44 */
45 protected $deletedEntities;
46
47 /**
48 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
49 */
50 protected $changedEntities;
51
52 /**
53 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
54 */
55 protected $visitedDuringPersistence;
56
57 /**
58 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
59 */
60 protected $reflectionService;
61
62 /**
63 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
64 */
65 protected $qomFactory;
66
67 /**
68 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface
69 */
70 protected $storageBackend;
71
72 /**
73 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
74 */
75 protected $dataMapper;
76
77 /**
78 * The TYPO3 reference index object
79 *
80 * @var \TYPO3\CMS\Core\Database\ReferenceIndex
81 */
82 protected $referenceIndex;
83
84 /**
85 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
86 */
87 protected $configurationManager;
88
89 /**
90 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
91 */
92 protected $signalSlotDispatcher;
93
94 /**
95 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Session $session
96 */
97 public function injectSession(\TYPO3\CMS\Extbase\Persistence\Generic\Session $session)
98 {
99 $this->session = $session;
100 }
101
102 /**
103 * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService
104 */
105 public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
106 {
107 $this->reflectionService = $reflectionService;
108 }
109
110 /**
111 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory $qomFactory
112 */
113 public function injectQomFactory(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory $qomFactory)
114 {
115 $this->qomFactory = $qomFactory;
116 }
117
118 /**
119 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface $storageBackend
120 */
121 public function injectStorageBackend(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface $storageBackend)
122 {
123 $this->storageBackend = $storageBackend;
124 }
125
126 /**
127 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper
128 */
129 public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
130 {
131 $this->dataMapper = $dataMapper;
132 }
133
134 /**
135 * @param \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher
136 */
137 public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
138 {
139 $this->signalSlotDispatcher = $signalSlotDispatcher;
140 }
141
142 /**
143 * Constructs the backend
144 *
145 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
146 */
147 public function __construct(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
148 {
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();
155 }
156
157 /**
158 * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager
159 */
160 public function setPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
161 {
162 $this->persistenceManager = $persistenceManager;
163 }
164
165 /**
166 * Returns the repository session
167 *
168 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Session
169 */
170 public function getSession()
171 {
172 return $this->session;
173 }
174
175 /**
176 * Returns the Data Mapper
177 *
178 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
179 */
180 public function getDataMapper()
181 {
182 return $this->dataMapper;
183 }
184
185 /**
186 * Returns the current QOM factory
187 *
188 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
189 */
190 public function getQomFactory()
191 {
192 return $this->qomFactory;
193 }
194
195 /**
196 * Returns the reflection service
197 *
198 * @return \TYPO3\CMS\Extbase\Reflection\ReflectionService
199 */
200 public function getReflectionService()
201 {
202 return $this->reflectionService;
203 }
204
205 /**
206 * Returns the number of records matching the query.
207 *
208 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
209 * @return int
210 * @api
211 */
212 public function getObjectCountByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query)
213 {
214 return $this->storageBackend->getObjectCountByQuery($query);
215 }
216
217 /**
218 * Returns the object data matching the $query.
219 *
220 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
221 * @return array
222 * @api
223 */
224 public function getObjectDataByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query)
225 {
226 $query = $this->emitBeforeGettingObjectDataSignal($query);
227 $result = $this->storageBackend->getObjectDataByQuery($query);
228 $result = $this->emitAfterGettingObjectDataSignal($query, $result);
229 return $result;
230 }
231
232 /**
233 * Emits a signal before object data is fetched
234 *
235 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
236 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface Modified query
237 */
238 protected function emitBeforeGettingObjectDataSignal(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query)
239 {
240 $signalArguments = $this->signalSlotDispatcher->dispatch(__CLASS__, 'beforeGettingObjectData', [$query]);
241 return $signalArguments[0];
242 }
243
244 /**
245 * Emits a signal after object data is fetched
246 *
247 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
248 * @param array $result
249 * @return array Modified result
250 */
251 protected function emitAfterGettingObjectDataSignal(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array $result)
252 {
253 $signalArguments = $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterGettingObjectData', [$query, $result]);
254 return $signalArguments[1];
255 }
256
257 /**
258 * Returns the (internal) identifier for the object, if it is known to the
259 * backend. Otherwise NULL is returned.
260 *
261 * @param object $object
262 * @return string|null The identifier for the object if it is known, or NULL
263 */
264 public function getIdentifierByObject($object)
265 {
266 if ($object instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
267 $object = $object->_loadRealInstance();
268 if (!is_object($object)) {
269 return null;
270 }
271 }
272 return $this->session->getIdentifierByObject($object);
273 }
274
275 /**
276 * Returns the object with the (internal) identifier, if it is known to the
277 * backend. Otherwise NULL is returned.
278 *
279 * @param string $identifier
280 * @param string $className
281 * @return object|null The object for the identifier if it is known, or NULL
282 */
283 public function getObjectByIdentifier($identifier, $className)
284 {
285 if ($this->session->hasIdentifier($identifier, $className)) {
286 return $this->session->getObjectByIdentifier($identifier, $className);
287 }
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();
292 }
293
294 /**
295 * Checks if the given object has ever been persisted.
296 *
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
299 */
300 public function isNewObject($object)
301 {
302 return $this->getIdentifierByObject($object) === null;
303 }
304
305 /**
306 * Sets the aggregate root objects
307 *
308 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $objects
309 */
310 public function setAggregateRootObjects(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $objects)
311 {
312 $this->aggregateRootObjects = $objects;
313 }
314
315 /**
316 * Sets the changed objects
317 *
318 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $entities
319 */
320 public function setChangedEntities(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $entities)
321 {
322 $this->changedEntities = $entities;
323 }
324
325 /**
326 * Sets the deleted objects
327 *
328 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $entities
329 */
330 public function setDeletedEntities(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $entities)
331 {
332 $this->deletedEntities = $entities;
333 }
334
335 /**
336 * Commits the current persistence session.
337 */
338 public function commit()
339 {
340 $this->persistObjects();
341 $this->processDeletedObjects();
342 }
343
344 /**
345 * Traverse and persist all aggregate roots and their object graph.
346 */
347 protected function persistObjects()
348 {
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);
354 }
355 $this->persistObject($object, null);
356 }
357 foreach ($this->changedEntities as $object) {
358 $this->persistObject($object, null);
359 }
360 }
361
362 /**
363 * Persists the given object.
364 *
365 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be inserted
366 */
367 protected function persistObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object)
368 {
369 if (isset($this->visitedDuringPersistence[$object])) {
370 return;
371 }
372 $row = [];
373 $queue = [];
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)) {
378 continue;
379 }
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();
389 }
390 foreach ($propertyValue as $containedObject) {
391 if ($containedObject instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
392 $queue[] = $containedObject;
393 }
394 }
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);
400 }
401 // Check explicitly for NULL, as getPlainValue would convert this to 'NULL'
402 $row[$columnMap->getColumnName()] = $propertyValue !== null
403 ? $this->dataMapper->getPlainValue($propertyValue)
404 : null;
405 }
406 $queue[] = $propertyValue;
407 } elseif ($object->_isNew() || $object->_isDirty($propertyName)) {
408 $row[$columnMap->getColumnName()] = $this->dataMapper->getPlainValue($propertyValue, $columnMap);
409 }
410 }
411 if (!empty($row)) {
412 $this->updateObject($object, $row);
413 $object->_memorizeCleanState();
414 }
415 $this->visitedDuringPersistence[$object] = $object->getUid();
416 foreach ($queue as $queuedObject) {
417 $this->persistObject($queuedObject);
418 }
419 $this->emitAfterPersistObjectSignal($object);
420 }
421
422 /**
423 * Checks, if the property value is lazy loaded and was not initialized
424 *
425 * @param mixed $propertyValue The property value
426 * @return bool
427 */
428 protected function propertyValueIsLazyLoaded($propertyValue)
429 {
430 if ($propertyValue instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
431 return true;
432 }
433 if ($propertyValue instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage) {
434 if ($propertyValue->isInitialized() === false) {
435 return true;
436 }
437 }
438 return false;
439 }
440
441 /**
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.
445 *
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.
450 */
451 protected function persistObjectStorage(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $objectStorage, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $propertyName, array &$row)
452 {
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);
460 }
461 }
462
463 $currentUids = [];
464 $sortingPosition = 1;
465 $updateSortingOfFollowing = false;
466
467 foreach ($objectStorage as $object) {
468 /** @var DomainObjectInterface $object */
469 if (empty($currentUids)) {
470 $sortingPosition = 1;
471 } else {
472 $sortingPosition++;
473 }
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);
491 } else {
492 $sortingPosition = $objectStorage->getPosition($object);
493 }
494 }
495 $currentUids[] = $object->getUid();
496 }
497
498 if ($columnMap->getParentKeyFieldName() === null) {
499 $row[$columnMap->getColumnName()] = implode(',', $currentUids);
500 } else {
501 $row[$columnMap->getColumnName()] = $this->dataMapper->countRelated($parentObject, $propertyName);
502 }
503 }
504
505 /**
506 * Returns the removed objects determined by a comparison of the clean property value
507 * with the actual property value.
508 *
509 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object
510 * @param string $propertyName
511 * @return array An array of removed objects
512 */
513 protected function getRemovedChildObjects(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, $propertyName)
514 {
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;
522 }
523 }
524 }
525 return $removedObjects;
526 }
527
528 /**
529 * Updates the fields defining the relation between the object and the parent object.
530 *
531 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
532 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject
533 * @param string $parentPropertyName
534 * @param int $sortingPosition
535 */
536 protected function attachObjectToParentObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName, $sortingPosition = 0)
537 {
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);
544 }
545 }
546
547 /**
548 * Updates the fields defining the relation between the object and the parent object.
549 *
550 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
551 * @param \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $parentObject
552 * @param string $parentPropertyName
553 * @param int $sortingPosition
554 */
555 protected function updateRelationOfObjectToParentObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $parentObject, $parentPropertyName, $sortingPosition = 0)
556 {
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);
563 }
564 }
565
566 /**
567 * Updates fields defining the relation between the object and the parent object in relation has-many.
568 *
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
574 */
575 protected function attachObjectToParentObjectRelationHasMany(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $parentObject, $parentPropertyName, $sortingPosition = 0)
576 {
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,
583 1345368105
584 );
585 }
586 $row = [];
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();
593 }
594 $relationTableMatchFields = $parentColumnMap->getRelationTableMatchFields();
595 if (is_array($relationTableMatchFields)) {
596 $row = array_merge($relationTableMatchFields, $row);
597 }
598 }
599 $childSortByFieldName = $parentColumnMap->getChildSortByFieldName();
600 if (!empty($childSortByFieldName)) {
601 $row[$childSortByFieldName] = $sortingPosition;
602 }
603 if (!empty($row)) {
604 $this->updateObject($object, $row);
605 }
606 }
607
608 /**
609 * Updates the fields defining the relation between the object and the parent object.
610 *
611 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
612 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject
613 * @param string $parentPropertyName
614 */
615 protected function detachObjectFromParentObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName)
616 {
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) {
620 $row = [];
621 $parentKeyFieldName = $parentColumnMap->getParentKeyFieldName();
622 if ($parentKeyFieldName !== null) {
623 $row[$parentKeyFieldName] = 0;
624 $parentTableFieldName = $parentColumnMap->getParentTableFieldName();
625 if ($parentTableFieldName !== null) {
626 $row[$parentTableFieldName] = '';
627 }
628 $relationTableMatchFields = $parentColumnMap->getRelationTableMatchFields();
629 if (is_array($relationTableMatchFields) && !empty($relationTableMatchFields)) {
630 $row = array_merge(array_fill_keys(array_keys($relationTableMatchFields), ''), $row);
631 }
632 }
633 $childSortByFieldName = $parentColumnMap->getChildSortByFieldName();
634 if (!empty($childSortByFieldName)) {
635 $row[$childSortByFieldName] = 0;
636 }
637 if (!empty($row)) {
638 $this->updateObject($object, $row);
639 }
640 } elseif ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
641 $this->deleteRelationFromRelationtable($object, $parentObject, $parentPropertyName);
642 }
643 }
644
645 /**
646 * Inserts an object in the storage backend
647 *
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
651 */
652 protected function insertObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject = null, $parentPropertyName = '')
653 {
654 if ($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject) {
655 $result = $this->getUidOfAlreadyPersistedValueObject($object);
656 if ($result !== false) {
657 $object->_setProperty('uid', (int)$result);
658 return;
659 }
660 }
661 $dataMap = $this->dataMapper->getDataMap(get_class($object));
662 $row = [];
663 $properties = $object->_getProperties();
664 foreach ($properties as $propertyName => $propertyValue) {
665 if (!$dataMap->isPersistableProperty($propertyName) || $this->propertyValueIsLazyLoaded($propertyValue)) {
666 continue;
667 }
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) {
673 // CSV type relation
674 $row[$columnMap->getColumnName()] = '';
675 } else {
676 // MM type relation
677 $row[$columnMap->getColumnName()] = 0;
678 }
679 } elseif ($propertyValue !== null) {
680 $row[$columnMap->getColumnName()] = $this->dataMapper->getPlainValue($propertyValue, $columnMap);
681 }
682 }
683 $this->addCommonFieldsToRow($object, $row);
684 if ($dataMap->getLanguageIdColumnName() !== null && $object->_getProperty('_languageUid') === null) {
685 $row[$dataMap->getLanguageIdColumnName()] = 0;
686 $object->_setProperty('_languageUid', 0);
687 }
688 if ($dataMap->getTranslationOriginColumnName() !== null) {
689 $row[$dataMap->getTranslationOriginColumnName()] = 0;
690 }
691 if ($dataMap->getTranslationOriginDiffSourceName() !== null) {
692 $row[$dataMap->getTranslationOriginDiffSourceName()] = '';
693 }
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);
699 }
700 if ($parentColumnDataMap->getParentKeyFieldName() !== null) {
701 $row[$parentColumnDataMap->getParentKeyFieldName()] = (int)$parentObject->getUid();
702 }
703 }
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);
709 }
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);
713 }
714 $this->session->registerObject($object, $uid);
715 if ((int)$uid >= 1) {
716 $this->emitEndInsertObjectSignal($object);
717 }
718 }
719
720 /**
721 * Emits a signal after an object was added to the storage
722 *
723 * @param DomainObjectInterface $object
724 */
725 protected function emitAfterInsertObjectSignal(DomainObjectInterface $object)
726 {
727 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterInsertObject', [$object]);
728 }
729
730 /**
731 * Emits a signal after an object was registered in persistence session
732 * This signal replaces the afterInsertObject signal which is now deprecated
733 *
734 * @param DomainObjectInterface $object
735 */
736 protected function emitEndInsertObjectSignal(DomainObjectInterface $object)
737 {
738 $this->signalSlotDispatcher->dispatch(__CLASS__, 'endInsertObject', [$object]);
739 }
740
741 /**
742 * Tests, if the given Value Object already exists in the storage backend and if so, it returns the uid.
743 *
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
746 */
747 protected function getUidOfAlreadyPersistedValueObject(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object)
748 {
749 return $this->storageBackend->getUidOfAlreadyPersistedValueObject($object);
750 }
751
752 /**
753 * Inserts mm-relation into a relation table
754 *
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
760 */
761 protected function insertRelationInRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $propertyName, $sortingPosition = null)
762 {
763 $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
764 $columnMap = $dataMap->getColumnMap($propertyName);
765 $parentUid = $parentObject->getUid();
766 if ($parentObject->_getProperty('_localizedUid') !== null) {
767 $parentUid = $parentObject->_getProperty('_localizedUid');
768 }
769 $row = [
770 $columnMap->getParentKeyFieldName() => (int)$parentUid,
771 $columnMap->getChildKeyFieldName() => (int)$object->getUid(),
772 $columnMap->getChildSortByFieldName() => !is_null($sortingPosition) ? (int)$sortingPosition : 0
773 ];
774 $relationTableName = $columnMap->getRelationTableName();
775 if ($columnMap->getRelationTablePageIdColumnName() !== null) {
776 $row[$columnMap->getRelationTablePageIdColumnName()] = $this->determineStoragePageIdForNewRecord();
777 }
778 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
779 if (is_array($relationTableMatchFields)) {
780 $row = array_merge($relationTableMatchFields, $row);
781 }
782 $relationTableInsertFields = $columnMap->getRelationTableInsertFields();
783 if (is_array($relationTableInsertFields)) {
784 $row = array_merge($relationTableInsertFields, $row);
785 }
786 $res = $this->storageBackend->addRow($relationTableName, $row, true);
787 return $res;
788 }
789
790 /**
791 * Updates mm-relation in a relation table
792 *
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
798 */
799 protected function updateRelationInRelationTable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $propertyName, $sortingPosition = 0)
800 {
801 $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
802 $columnMap = $dataMap->getColumnMap($propertyName);
803 $row = [
804 $columnMap->getParentKeyFieldName() => (int)$parentObject->getUid(),
805 $columnMap->getChildKeyFieldName() => (int)$object->getUid(),
806 $columnMap->getChildSortByFieldName() => (int)$sortingPosition
807 ];
808 $relationTableName = $columnMap->getRelationTableName();
809 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
810 if (is_array($relationTableMatchFields)) {
811 $row = array_merge($relationTableMatchFields, $row);
812 }
813 $res = $this->storageBackend->updateRelationTableRow(
814 $relationTableName,
815 $row
816 );
817 return $res;
818 }
819
820 /**
821 * Delete all mm-relations of a parent from a relation table
822 *
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
826 */
827 protected function deleteAllRelationsFromRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName)
828 {
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()
834 ];
835 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
836 if (is_array($relationTableMatchFields)) {
837 $relationMatchFields = array_merge($relationTableMatchFields, $relationMatchFields);
838 }
839 $res = $this->storageBackend->removeRow($relationTableName, $relationMatchFields, false);
840 return $res;
841 }
842
843 /**
844 * Delete an mm-relation from a relation table
845 *
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
849 * @return bool
850 */
851 protected function deleteRelationFromRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $relatedObject, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName)
852 {
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()
859 ];
860 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
861 if (is_array($relationTableMatchFields)) {
862 $relationMatchFields = array_merge($relationTableMatchFields, $relationMatchFields);
863 }
864 $res = $this->storageBackend->removeRow($relationTableName, $relationMatchFields, false);
865 return $res;
866 }
867
868 /**
869 * Fetches maximal value currently used for sorting field in parent table
870 *
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
875 */
876 protected function fetchMaxSortingFromParentTable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName)
877 {
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();
883
884 if (empty($sortByFieldName)) {
885 return false;
886 }
887 $matchFields = [];
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();
894 }
895 }
896
897 if (empty($matchFields)) {
898 return false;
899 }
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();
903
904 $matchFields = [
905 $parentColumnMap->getParentKeyFieldName() => (int)$parentObject->getUid()
906 ];
907
908 $relationTableMatchFields = $parentColumnMap->getRelationTableMatchFields();
909 if (is_array($relationTableMatchFields)) {
910 $matchFields = array_merge($relationTableMatchFields, $matchFields);
911 }
912 } else {
913 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalRelationTypeException('Unexpected parent column relation type: ' . $parentColumnMap->getTypeOfRelation(), 1345368106);
914 }
915
916 $result = $this->storageBackend->getMaxValueFromTable(
917 $tableName,
918 $matchFields,
919 $sortByFieldName
920 );
921 return $result;
922 }
923
924 /**
925 * Updates a given object in the storage
926 *
927 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be updated
928 * @param array $row Row to be stored
929 * @return bool
930 */
931 protected function updateObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, array $row)
932 {
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 ($object->_getProperty('_localizedUid') !== null) {
939 $row['uid'] = $object->_getProperty('_localizedUid');
940 }
941 }
942 $res = $this->storageBackend->updateRow($dataMap->getTableName(), $row);
943 if ($res === true) {
944 $this->emitAfterUpdateObjectSignal($object);
945 }
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']);
949 }
950 return $res;
951 }
952
953 /**
954 * Emits a signal after an object was updated in storage
955 *
956 * @param DomainObjectInterface $object
957 */
958 protected function emitAfterUpdateObjectSignal(DomainObjectInterface $object)
959 {
960 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterUpdateObject', [$object]);
961 }
962
963 /**
964 * Emits a signal after an object was persisted
965 *
966 * @param DomainObjectInterface $object
967 */
968 protected function emitAfterPersistObjectSignal(DomainObjectInterface $object)
969 {
970 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterPersistObject', [$object]);
971 }
972
973 /**
974 * Adds common databse fields to a row
975 *
976 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
977 * @param array &$row
978 */
979 protected function addCommonFieldsToRow(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, array &$row)
980 {
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();
985 }
986 if ($object->_isNew() && !isset($row['pid'])) {
987 $row['pid'] = $this->determineStoragePageIdForNewRecord($object);
988 }
989 }
990
991 /**
992 * Adjustes the common date fields of the given row to the current time
993 *
994 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
995 * @param array &$row The row to be updated
996 */
997 protected function addCommonDateFieldsToRow(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, array &$row)
998 {
999 $dataMap = $this->dataMapper->getDataMap(get_class($object));
1000 if ($object->_isNew() && $dataMap->getCreationDateColumnName() !== null) {
1001 $row[$dataMap->getCreationDateColumnName()] = $GLOBALS['EXEC_TIME'];
1002 }
1003 if ($dataMap->getModificationDateColumnName() !== null) {
1004 $row[$dataMap->getModificationDateColumnName()] = $GLOBALS['EXEC_TIME'];
1005 }
1006 }
1007
1008 /**
1009 * Iterate over deleted aggregate root objects and process them
1010 */
1011 protected function processDeletedObjects()
1012 {
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);
1018 }
1019 }
1020 $this->deletedEntities = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
1021 }
1022
1023 /**
1024 * Deletes an object
1025 *
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
1028 */
1029 protected function removeEntity(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, $markAsDeleted = true)
1030 {
1031 $dataMap = $this->dataMapper->getDataMap(get_class($object));
1032 $tableName = $dataMap->getTableName();
1033 if ($markAsDeleted === true && $dataMap->getDeletedFlagColumnName() !== null) {
1034 $deletedColumnName = $dataMap->getDeletedFlagColumnName();
1035 $row = [
1036 'uid' => $object->getUid(),
1037 $deletedColumnName => 1
1038 ];
1039 $this->addCommonDateFieldsToRow($object, $row);
1040 $res = $this->storageBackend->updateRow($tableName, $row);
1041 } else {
1042 $res = $this->storageBackend->removeRow($tableName, ['uid' => $object->getUid()]);
1043 }
1044 if ($res === true) {
1045 $this->emitAfterRemoveObjectSignal($object);
1046 }
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());
1051 }
1052 }
1053
1054 /**
1055 * Emits a signal after an object was removed from storage
1056 *
1057 * @param DomainObjectInterface $object
1058 */
1059 protected function emitAfterRemoveObjectSignal(DomainObjectInterface $object)
1060 {
1061 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterRemoveObject', [$object]);
1062 }
1063
1064 /**
1065 * Remove related objects
1066 *
1067 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to scanned for related objects
1068 */
1069 protected function removeRelatedObjects(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object)
1070 {
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) {
1078 continue;
1079 }
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);
1085 }
1086 } elseif ($propertyValue instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
1087 $this->removeEntity($propertyValue);
1088 }
1089 } elseif ($dataMap->getDeletedFlagColumnName() === null
1090 && $columnMap->getTypeOfRelation() === ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY
1091 ) {
1092 $this->deleteAllRelationsFromRelationtable($object, $propertyName);
1093 }
1094 }
1095 }
1096
1097 /**
1098 * Determine the storage page ID for a given NEW record
1099 *
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.
1104 *
1105 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
1106 * @return int the storage Page ID where the object should be stored
1107 */
1108 protected function determineStoragePageIdForNewRecord(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object = null)
1109 {
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');
1114 if (isset($pid)) {
1115 return (int)$pid;
1116 }
1117 }
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'];
1121 }
1122 }
1123 $storagePidList = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $frameworkConfiguration['persistence']['storagePid']);
1124 return (int)$storagePidList[0];
1125 }
1126 }