5217ec0e5197bb51761484d33e17549ff8ff2c1a
[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->aggregateRootObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
152 $this->deletedEntities = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
153 $this->changedEntities = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
154 }
155
156 /**
157 * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager
158 */
159 public function setPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
160 {
161 $this->persistenceManager = $persistenceManager;
162 }
163
164 /**
165 * Returns the repository session
166 *
167 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Session
168 */
169 public function getSession()
170 {
171 return $this->session;
172 }
173
174 /**
175 * Returns the Data Mapper
176 *
177 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
178 */
179 public function getDataMapper()
180 {
181 return $this->dataMapper;
182 }
183
184 /**
185 * Returns the current QOM factory
186 *
187 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
188 */
189 public function getQomFactory()
190 {
191 return $this->qomFactory;
192 }
193
194 /**
195 * Returns the reflection service
196 *
197 * @return \TYPO3\CMS\Extbase\Reflection\ReflectionService
198 */
199 public function getReflectionService()
200 {
201 return $this->reflectionService;
202 }
203
204 /**
205 * Returns the number of records matching the query.
206 *
207 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
208 * @return int
209 * @api
210 */
211 public function getObjectCountByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query)
212 {
213 return $this->storageBackend->getObjectCountByQuery($query);
214 }
215
216 /**
217 * Returns the object data matching the $query.
218 *
219 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
220 * @return array
221 * @api
222 */
223 public function getObjectDataByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query)
224 {
225 $query = $this->emitBeforeGettingObjectDataSignal($query);
226 $result = $this->storageBackend->getObjectDataByQuery($query);
227 $result = $this->emitAfterGettingObjectDataSignal($query, $result);
228 return $result;
229 }
230
231 /**
232 * Emits a signal before object data is fetched
233 *
234 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
235 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface Modified query
236 */
237 protected function emitBeforeGettingObjectDataSignal(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query)
238 {
239 $signalArguments = $this->signalSlotDispatcher->dispatch(__CLASS__, 'beforeGettingObjectData', [$query]);
240 return $signalArguments[0];
241 }
242
243 /**
244 * Emits a signal after object data is fetched
245 *
246 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
247 * @param array $result
248 * @return array Modified result
249 */
250 protected function emitAfterGettingObjectDataSignal(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array $result)
251 {
252 $signalArguments = $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterGettingObjectData', [$query, $result]);
253 return $signalArguments[1];
254 }
255
256 /**
257 * Returns the (internal) identifier for the object, if it is known to the
258 * backend. Otherwise NULL is returned.
259 *
260 * @param object $object
261 * @return string|null The identifier for the object if it is known, or NULL
262 */
263 public function getIdentifierByObject($object)
264 {
265 if ($object instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
266 $object = $object->_loadRealInstance();
267 if (!is_object($object)) {
268 return null;
269 }
270 }
271 return $this->session->getIdentifierByObject($object);
272 }
273
274 /**
275 * Returns the object with the (internal) identifier, if it is known to the
276 * backend. Otherwise NULL is returned.
277 *
278 * @param string $identifier
279 * @param string $className
280 * @return object|null The object for the identifier if it is known, or NULL
281 */
282 public function getObjectByIdentifier($identifier, $className)
283 {
284 if ($this->session->hasIdentifier($identifier, $className)) {
285 return $this->session->getObjectByIdentifier($identifier, $className);
286 }
287 $query = $this->persistenceManager->createQueryForType($className);
288 $query->getQuerySettings()->setRespectStoragePage(false);
289 $query->getQuerySettings()->setRespectSysLanguage(false);
290 return $query->matching($query->equals('uid', $identifier))->execute()->getFirst();
291 }
292
293 /**
294 * Checks if the given object has ever been persisted.
295 *
296 * @param object $object The object to check
297 * @return bool TRUE if the object is new, FALSE if the object exists in the repository
298 */
299 public function isNewObject($object)
300 {
301 return $this->getIdentifierByObject($object) === null;
302 }
303
304 /**
305 * Sets the aggregate root objects
306 *
307 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $objects
308 */
309 public function setAggregateRootObjects(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $objects)
310 {
311 $this->aggregateRootObjects = $objects;
312 }
313
314 /**
315 * Sets the changed objects
316 *
317 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $entities
318 */
319 public function setChangedEntities(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $entities)
320 {
321 $this->changedEntities = $entities;
322 }
323
324 /**
325 * Sets the deleted objects
326 *
327 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $entities
328 */
329 public function setDeletedEntities(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $entities)
330 {
331 $this->deletedEntities = $entities;
332 }
333
334 /**
335 * Commits the current persistence session.
336 */
337 public function commit()
338 {
339 $this->persistObjects();
340 $this->processDeletedObjects();
341 }
342
343 /**
344 * Traverse and persist all aggregate roots and their object graph.
345 */
346 protected function persistObjects()
347 {
348 $this->visitedDuringPersistence = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
349 foreach ($this->aggregateRootObjects as $object) {
350 /** @var DomainObjectInterface $object */
351 if ($object->_isNew()) {
352 $this->insertObject($object);
353 }
354 $this->persistObject($object, null);
355 }
356 foreach ($this->changedEntities as $object) {
357 $this->persistObject($object, null);
358 }
359 }
360
361 /**
362 * Persists the given object.
363 *
364 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be inserted
365 */
366 protected function persistObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object)
367 {
368 if (isset($this->visitedDuringPersistence[$object])) {
369 return;
370 }
371 $row = [];
372 $queue = [];
373 $dataMap = $this->dataMapper->getDataMap(get_class($object));
374 $properties = $object->_getProperties();
375 foreach ($properties as $propertyName => $propertyValue) {
376 if (!$dataMap->isPersistableProperty($propertyName) || $this->propertyValueIsLazyLoaded($propertyValue)) {
377 continue;
378 }
379 $columnMap = $dataMap->getColumnMap($propertyName);
380 if ($propertyValue instanceof \TYPO3\CMS\Extbase\Persistence\ObjectStorage) {
381 $cleanProperty = $object->_getCleanProperty($propertyName);
382 // objectstorage needs to be persisted if the object is new, the objectstorge is dirty, meaning it has
383 // been changed after initial build, or an empty objectstorge is present and the cleanstate objectstorage
384 // has childelements, meaning all elements should been removed from the objectstorage
385 if ($object->_isNew() || $propertyValue->_isDirty() || ($propertyValue->count() === 0 && $cleanProperty && $cleanProperty->count() > 0)) {
386 $this->persistObjectStorage($propertyValue, $object, $propertyName, $row);
387 $propertyValue->_memorizeCleanState();
388 }
389 foreach ($propertyValue as $containedObject) {
390 if ($containedObject instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
391 $queue[] = $containedObject;
392 }
393 }
394 } elseif ($propertyValue instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
395 && $object instanceof ObjectMonitoringInterface) {
396 if ($object->_isDirty($propertyName)) {
397 if ($propertyValue->_isNew()) {
398 $this->insertObject($propertyValue, $object, $propertyName);
399 }
400 // Check explicitly for NULL, as getPlainValue would convert this to 'NULL'
401 $row[$columnMap->getColumnName()] = $propertyValue !== null
402 ? $this->dataMapper->getPlainValue($propertyValue)
403 : null;
404 }
405 $queue[] = $propertyValue;
406 } elseif ($object->_isNew() || $object->_isDirty($propertyName)) {
407 $row[$columnMap->getColumnName()] = $this->dataMapper->getPlainValue($propertyValue, $columnMap);
408 }
409 }
410 if (!empty($row)) {
411 $this->updateObject($object, $row);
412 $object->_memorizeCleanState();
413 }
414 $this->visitedDuringPersistence[$object] = $object->getUid();
415 foreach ($queue as $queuedObject) {
416 $this->persistObject($queuedObject);
417 }
418 $this->emitAfterPersistObjectSignal($object);
419 }
420
421 /**
422 * Checks, if the property value is lazy loaded and was not initialized
423 *
424 * @param mixed $propertyValue The property value
425 * @return bool
426 */
427 protected function propertyValueIsLazyLoaded($propertyValue)
428 {
429 if ($propertyValue instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
430 return true;
431 }
432 if ($propertyValue instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage) {
433 if ($propertyValue->isInitialized() === false) {
434 return true;
435 }
436 }
437 return false;
438 }
439
440 /**
441 * 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
442 * gets persisted immediately. Objects which were removed from the property were detached from the parent object. They will not be
443 * deleted by default. You have to annotate the property with "@cascade remove" if you want them to be deleted as well.
444 *
445 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $objectStorage The object storage to be persisted.
446 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object. One of the properties holds the object storage.
447 * @param string $propertyName The name of the property holding the object storage.
448 * @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.
449 */
450 protected function persistObjectStorage(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $objectStorage, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $propertyName, array &$row)
451 {
452 $className = get_class($parentObject);
453 $columnMap = $this->dataMapper->getDataMap($className)->getColumnMap($propertyName);
454 $propertyMetaData = $this->reflectionService->getClassSchema($className)->getProperty($propertyName);
455 foreach ($this->getRemovedChildObjects($parentObject, $propertyName) as $removedObject) {
456 $this->detachObjectFromParentObject($removedObject, $parentObject, $propertyName);
457 if ($columnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY && $propertyMetaData['cascade'] === 'remove') {
458 $this->removeEntity($removedObject);
459 }
460 }
461
462 $currentUids = [];
463 $sortingPosition = 1;
464 $updateSortingOfFollowing = false;
465
466 foreach ($objectStorage as $object) {
467 /** @var DomainObjectInterface $object */
468 if (empty($currentUids)) {
469 $sortingPosition = 1;
470 } else {
471 $sortingPosition++;
472 }
473 $cleanProperty = $parentObject->_getCleanProperty($propertyName);
474 if ($object->_isNew()) {
475 $this->insertObject($object);
476 $this->attachObjectToParentObject($object, $parentObject, $propertyName, $sortingPosition);
477 // if a new object is inserted, all objects after this need to have their sorting updated
478 $updateSortingOfFollowing = true;
479 } elseif ($cleanProperty === null || $cleanProperty->getPosition($object) === null) {
480 // if parent object is new then it doesn't have cleanProperty yet; before attaching object it's clean position is null
481 $this->attachObjectToParentObject($object, $parentObject, $propertyName, $sortingPosition);
482 // 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
483 $updateSortingOfFollowing = true;
484 } elseif ($objectStorage->isRelationDirty($object) || $cleanProperty->getPosition($object) !== $objectStorage->getPosition($object)) {
485 $this->updateRelationOfObjectToParentObject($object, $parentObject, $propertyName, $sortingPosition);
486 $updateSortingOfFollowing = true;
487 } elseif ($updateSortingOfFollowing) {
488 if ($sortingPosition > $objectStorage->getPosition($object)) {
489 $this->updateRelationOfObjectToParentObject($object, $parentObject, $propertyName, $sortingPosition);
490 } else {
491 $sortingPosition = $objectStorage->getPosition($object);
492 }
493 }
494 $currentUids[] = $object->getUid();
495 }
496
497 if ($columnMap->getParentKeyFieldName() === null) {
498 $row[$columnMap->getColumnName()] = implode(',', $currentUids);
499 } else {
500 $row[$columnMap->getColumnName()] = $this->dataMapper->countRelated($parentObject, $propertyName);
501 }
502 }
503
504 /**
505 * Returns the removed objects determined by a comparison of the clean property value
506 * with the actual property value.
507 *
508 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object
509 * @param string $propertyName
510 * @return array An array of removed objects
511 */
512 protected function getRemovedChildObjects(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, $propertyName)
513 {
514 $removedObjects = [];
515 $cleanPropertyValue = $object->_getCleanProperty($propertyName);
516 if (is_array($cleanPropertyValue) || $cleanPropertyValue instanceof \Iterator) {
517 $propertyValue = $object->_getProperty($propertyName);
518 foreach ($cleanPropertyValue as $containedObject) {
519 if (!$propertyValue->contains($containedObject)) {
520 $removedObjects[] = $containedObject;
521 }
522 }
523 }
524 return $removedObjects;
525 }
526
527 /**
528 * Updates the fields defining the relation between the object and the parent object.
529 *
530 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
531 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject
532 * @param string $parentPropertyName
533 * @param int $sortingPosition
534 */
535 protected function attachObjectToParentObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName, $sortingPosition = 0)
536 {
537 $parentDataMap = $this->dataMapper->getDataMap(get_class($parentObject));
538 $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
539 if ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY) {
540 $this->attachObjectToParentObjectRelationHasMany($object, $parentObject, $parentPropertyName, $sortingPosition);
541 } elseif ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
542 $this->insertRelationInRelationtable($object, $parentObject, $parentPropertyName, $sortingPosition);
543 }
544 }
545
546 /**
547 * Updates the fields defining the relation between the object and the parent object.
548 *
549 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
550 * @param \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $parentObject
551 * @param string $parentPropertyName
552 * @param int $sortingPosition
553 */
554 protected function updateRelationOfObjectToParentObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $parentObject, $parentPropertyName, $sortingPosition = 0)
555 {
556 $parentDataMap = $this->dataMapper->getDataMap(get_class($parentObject));
557 $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
558 if ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY) {
559 $this->attachObjectToParentObjectRelationHasMany($object, $parentObject, $parentPropertyName, $sortingPosition);
560 } elseif ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
561 $this->updateRelationInRelationTable($object, $parentObject, $parentPropertyName, $sortingPosition);
562 }
563 }
564
565 /**
566 * Updates fields defining the relation between the object and the parent object in relation has-many.
567 *
568 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
569 * @param \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $parentObject
570 * @param string $parentPropertyName
571 * @param int $sortingPosition
572 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalRelationTypeException
573 */
574 protected function attachObjectToParentObjectRelationHasMany(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $parentObject, $parentPropertyName, $sortingPosition = 0)
575 {
576 $parentDataMap = $this->dataMapper->getDataMap(get_class($parentObject));
577 $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
578 if ($parentColumnMap->getTypeOfRelation() !== \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY) {
579 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalRelationTypeException(
580 'Parent column relation type is ' . $parentColumnMap->getTypeOfRelation() .
581 ' but should be ' . \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY,
582 1345368105
583 );
584 }
585 $row = [];
586 $parentKeyFieldName = $parentColumnMap->getParentKeyFieldName();
587 if ($parentKeyFieldName !== null) {
588 $row[$parentKeyFieldName] = $parentObject->getUid();
589 $parentTableFieldName = $parentColumnMap->getParentTableFieldName();
590 if ($parentTableFieldName !== null) {
591 $row[$parentTableFieldName] = $parentDataMap->getTableName();
592 }
593 $relationTableMatchFields = $parentColumnMap->getRelationTableMatchFields();
594 if (is_array($relationTableMatchFields)) {
595 $row = array_merge($relationTableMatchFields, $row);
596 }
597 }
598 $childSortByFieldName = $parentColumnMap->getChildSortByFieldName();
599 if (!empty($childSortByFieldName)) {
600 $row[$childSortByFieldName] = $sortingPosition;
601 }
602 if (!empty($row)) {
603 $this->updateObject($object, $row);
604 }
605 }
606
607 /**
608 * Updates the fields defining the relation between the object and the parent object.
609 *
610 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
611 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject
612 * @param string $parentPropertyName
613 */
614 protected function detachObjectFromParentObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName)
615 {
616 $parentDataMap = $this->dataMapper->getDataMap(get_class($parentObject));
617 $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
618 if ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY) {
619 $row = [];
620 $parentKeyFieldName = $parentColumnMap->getParentKeyFieldName();
621 if ($parentKeyFieldName !== null) {
622 $row[$parentKeyFieldName] = 0;
623 $parentTableFieldName = $parentColumnMap->getParentTableFieldName();
624 if ($parentTableFieldName !== null) {
625 $row[$parentTableFieldName] = '';
626 }
627 $relationTableMatchFields = $parentColumnMap->getRelationTableMatchFields();
628 if (is_array($relationTableMatchFields) && !empty($relationTableMatchFields)) {
629 $row = array_merge(array_fill_keys(array_keys($relationTableMatchFields), ''), $row);
630 }
631 }
632 $childSortByFieldName = $parentColumnMap->getChildSortByFieldName();
633 if (!empty($childSortByFieldName)) {
634 $row[$childSortByFieldName] = 0;
635 }
636 if (!empty($row)) {
637 $this->updateObject($object, $row);
638 }
639 } elseif ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
640 $this->deleteRelationFromRelationtable($object, $parentObject, $parentPropertyName);
641 }
642 }
643
644 /**
645 * Inserts an object in the storage backend
646 *
647 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be insterted in the storage
648 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parentobject.
649 * @param string $parentPropertyName
650 */
651 protected function insertObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject = null, $parentPropertyName = '')
652 {
653 if ($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject) {
654 $result = $this->getUidOfAlreadyPersistedValueObject($object);
655 if ($result !== false) {
656 $object->_setProperty('uid', (int)$result);
657 return;
658 }
659 }
660 $dataMap = $this->dataMapper->getDataMap(get_class($object));
661 $row = [];
662 $properties = $object->_getProperties();
663 foreach ($properties as $propertyName => $propertyValue) {
664 if (!$dataMap->isPersistableProperty($propertyName) || $this->propertyValueIsLazyLoaded($propertyValue)) {
665 continue;
666 }
667 $columnMap = $dataMap->getColumnMap($propertyName);
668 if ($columnMap->getTypeOfRelation() === ColumnMap::RELATION_HAS_ONE) {
669 $row[$columnMap->getColumnName()] = 0;
670 } elseif ($columnMap->getTypeOfRelation() !== ColumnMap::RELATION_NONE) {
671 if ($columnMap->getParentKeyFieldName() === null) {
672 // CSV type relation
673 $row[$columnMap->getColumnName()] = '';
674 } else {
675 // MM type relation
676 $row[$columnMap->getColumnName()] = 0;
677 }
678 } elseif ($propertyValue !== null) {
679 $row[$columnMap->getColumnName()] = $this->dataMapper->getPlainValue($propertyValue, $columnMap);
680 }
681 }
682 $this->addCommonFieldsToRow($object, $row);
683 if ($dataMap->getLanguageIdColumnName() !== null && $object->_getProperty('_languageUid') === null) {
684 $row[$dataMap->getLanguageIdColumnName()] = 0;
685 $object->_setProperty('_languageUid', 0);
686 }
687 if ($dataMap->getTranslationOriginColumnName() !== null) {
688 $row[$dataMap->getTranslationOriginColumnName()] = 0;
689 }
690 if ($dataMap->getTranslationOriginDiffSourceName() !== null) {
691 $row[$dataMap->getTranslationOriginDiffSourceName()] = '';
692 }
693 if ($parentObject !== null && $parentPropertyName) {
694 $parentColumnDataMap = $this->dataMapper->getDataMap(get_class($parentObject))->getColumnMap($parentPropertyName);
695 $relationTableMatchFields = $parentColumnDataMap->getRelationTableMatchFields();
696 if (is_array($relationTableMatchFields)) {
697 $row = array_merge($relationTableMatchFields, $row);
698 }
699 if ($parentColumnDataMap->getParentKeyFieldName() !== null) {
700 $row[$parentColumnDataMap->getParentKeyFieldName()] = (int)$parentObject->getUid();
701 }
702 }
703 $uid = $this->storageBackend->addRow($dataMap->getTableName(), $row);
704 $object->_setProperty('uid', (int)$uid);
705 $object->setPid((int)$row['pid']);
706 if ((int)$uid >= 1) {
707 $this->emitAfterInsertObjectSignal($object);
708 }
709 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
710 if ($frameworkConfiguration['persistence']['updateReferenceIndex'] === '1') {
711 $this->referenceIndex->updateRefIndexTable($dataMap->getTableName(), $uid);
712 }
713 $this->session->registerObject($object, $uid);
714 if ((int)$uid >= 1) {
715 $this->emitEndInsertObjectSignal($object);
716 }
717 }
718
719 /**
720 * Emits a signal after an object was added to the storage
721 *
722 * @param DomainObjectInterface $object
723 */
724 protected function emitAfterInsertObjectSignal(DomainObjectInterface $object)
725 {
726 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterInsertObject', [$object]);
727 }
728
729 /**
730 * Emits a signal after an object was registered in persistence session
731 * This signal replaces the afterInsertObject signal which is now deprecated
732 *
733 * @param DomainObjectInterface $object
734 */
735 protected function emitEndInsertObjectSignal(DomainObjectInterface $object)
736 {
737 $this->signalSlotDispatcher->dispatch(__CLASS__, 'endInsertObject', [$object]);
738 }
739
740 /**
741 * Tests, if the given Value Object already exists in the storage backend and if so, it returns the uid.
742 *
743 * @param \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object The object to be tested
744 * @return mixed The matching uid if an object was found, else FALSE
745 */
746 protected function getUidOfAlreadyPersistedValueObject(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object)
747 {
748 return $this->storageBackend->getUidOfAlreadyPersistedValueObject($object);
749 }
750
751 /**
752 * Inserts mm-relation into a relation table
753 *
754 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The related object
755 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
756 * @param string $propertyName The name of the parent object's property where the related objects are stored in
757 * @param int $sortingPosition Defaults to NULL
758 * @return int The uid of the inserted row
759 */
760 protected function insertRelationInRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $propertyName, $sortingPosition = null)
761 {
762 $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
763 $columnMap = $dataMap->getColumnMap($propertyName);
764 $parentUid = $parentObject->getUid();
765 if ($parentObject->_getProperty('_localizedUid') !== null) {
766 $parentUid = $parentObject->_getProperty('_localizedUid');
767 }
768 $row = [
769 $columnMap->getParentKeyFieldName() => (int)$parentUid,
770 $columnMap->getChildKeyFieldName() => (int)$object->getUid(),
771 $columnMap->getChildSortByFieldName() => !is_null($sortingPosition) ? (int)$sortingPosition : 0
772 ];
773 $relationTableName = $columnMap->getRelationTableName();
774 if ($columnMap->getRelationTablePageIdColumnName() !== null) {
775 $row[$columnMap->getRelationTablePageIdColumnName()] = $this->determineStoragePageIdForNewRecord();
776 }
777 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
778 if (is_array($relationTableMatchFields)) {
779 $row = array_merge($relationTableMatchFields, $row);
780 }
781 $relationTableInsertFields = $columnMap->getRelationTableInsertFields();
782 if (is_array($relationTableInsertFields)) {
783 $row = array_merge($relationTableInsertFields, $row);
784 }
785 $res = $this->storageBackend->addRow($relationTableName, $row, true);
786 return $res;
787 }
788
789 /**
790 * Updates mm-relation in a relation table
791 *
792 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The related object
793 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
794 * @param string $propertyName The name of the parent object's property where the related objects are stored in
795 * @param int $sortingPosition Defaults to NULL
796 * @return bool TRUE if update was successfully
797 */
798 protected function updateRelationInRelationTable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $propertyName, $sortingPosition = 0)
799 {
800 $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
801 $columnMap = $dataMap->getColumnMap($propertyName);
802 $row = [
803 $columnMap->getParentKeyFieldName() => (int)$parentObject->getUid(),
804 $columnMap->getChildKeyFieldName() => (int)$object->getUid(),
805 $columnMap->getChildSortByFieldName() => (int)$sortingPosition
806 ];
807 $relationTableName = $columnMap->getRelationTableName();
808 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
809 if (is_array($relationTableMatchFields)) {
810 $row = array_merge($relationTableMatchFields, $row);
811 }
812 $res = $this->storageBackend->updateRelationTableRow(
813 $relationTableName,
814 $row
815 );
816 return $res;
817 }
818
819 /**
820 * Delete all mm-relations of a parent from a relation table
821 *
822 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
823 * @param string $parentPropertyName The name of the parent object's property where the related objects are stored in
824 * @return bool TRUE if delete was successfully
825 */
826 protected function deleteAllRelationsFromRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName)
827 {
828 $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
829 $columnMap = $dataMap->getColumnMap($parentPropertyName);
830 $relationTableName = $columnMap->getRelationTableName();
831 $relationMatchFields = [
832 $columnMap->getParentKeyFieldName() => (int)$parentObject->getUid()
833 ];
834 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
835 if (is_array($relationTableMatchFields)) {
836 $relationMatchFields = array_merge($relationTableMatchFields, $relationMatchFields);
837 }
838 $res = $this->storageBackend->removeRow($relationTableName, $relationMatchFields, false);
839 return $res;
840 }
841
842 /**
843 * Delete an mm-relation from a relation table
844 *
845 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $relatedObject The related object
846 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
847 * @param string $parentPropertyName The name of the parent object's property where the related objects are stored in
848 * @return bool
849 */
850 protected function deleteRelationFromRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $relatedObject, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName)
851 {
852 $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
853 $columnMap = $dataMap->getColumnMap($parentPropertyName);
854 $relationTableName = $columnMap->getRelationTableName();
855 $relationMatchFields = [
856 $columnMap->getParentKeyFieldName() => (int)$parentObject->getUid(),
857 $columnMap->getChildKeyFieldName() => (int)$relatedObject->getUid()
858 ];
859 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
860 if (is_array($relationTableMatchFields)) {
861 $relationMatchFields = array_merge($relationTableMatchFields, $relationMatchFields);
862 }
863 $res = $this->storageBackend->removeRow($relationTableName, $relationMatchFields, false);
864 return $res;
865 }
866
867 /**
868 * Fetches maximal value currently used for sorting field in parent table
869 *
870 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
871 * @param string $parentPropertyName The name of the parent object's property where the related objects are stored in
872 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalRelationTypeException
873 * @return mixed the max value
874 */
875 protected function fetchMaxSortingFromParentTable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName)
876 {
877 $parentDataMap = $this->dataMapper->getDataMap(get_class($parentObject));
878 $parentColumnMap = $parentDataMap->getColumnMap($parentPropertyName);
879 if ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY) {
880 $tableName = $parentColumnMap->getChildTableName();
881 $sortByFieldName = $parentColumnMap->getChildSortByFieldName();
882
883 if (empty($sortByFieldName)) {
884 return false;
885 }
886 $matchFields = [];
887 $parentKeyFieldName = $parentColumnMap->getParentKeyFieldName();
888 if ($parentKeyFieldName !== null) {
889 $matchFields[$parentKeyFieldName] = $parentObject->getUid();
890 $parentTableFieldName = $parentColumnMap->getParentTableFieldName();
891 if ($parentTableFieldName !== null) {
892 $matchFields[$parentTableFieldName] = $parentDataMap->getTableName();
893 }
894 }
895
896 if (empty($matchFields)) {
897 return false;
898 }
899 } elseif ($parentColumnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
900 $tableName = $parentColumnMap->getRelationTableName();
901 $sortByFieldName = $parentColumnMap->getChildSortByFieldName();
902
903 $matchFields = [
904 $parentColumnMap->getParentKeyFieldName() => (int)$parentObject->getUid()
905 ];
906
907 $relationTableMatchFields = $parentColumnMap->getRelationTableMatchFields();
908 if (is_array($relationTableMatchFields)) {
909 $matchFields = array_merge($relationTableMatchFields, $matchFields);
910 }
911 } else {
912 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalRelationTypeException('Unexpected parent column relation type: ' . $parentColumnMap->getTypeOfRelation(), 1345368106);
913 }
914
915 $result = $this->storageBackend->getMaxValueFromTable(
916 $tableName,
917 $matchFields,
918 $sortByFieldName
919 );
920 return $result;
921 }
922
923 /**
924 * Updates a given object in the storage
925 *
926 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be updated
927 * @param array $row Row to be stored
928 * @return bool
929 */
930 protected function updateObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, array $row)
931 {
932 $dataMap = $this->dataMapper->getDataMap(get_class($object));
933 $this->addCommonFieldsToRow($object, $row);
934 $row['uid'] = $object->getUid();
935 if ($dataMap->getLanguageIdColumnName() !== null) {
936 $row[$dataMap->getLanguageIdColumnName()] = (int)$object->_getProperty('_languageUid');
937 if ($object->_getProperty('_localizedUid') !== null) {
938 $row['uid'] = $object->_getProperty('_localizedUid');
939 }
940 }
941 $res = $this->storageBackend->updateRow($dataMap->getTableName(), $row);
942 if ($res === true) {
943 $this->emitAfterUpdateObjectSignal($object);
944 }
945 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
946 if ($frameworkConfiguration['persistence']['updateReferenceIndex'] === '1') {
947 $this->referenceIndex->updateRefIndexTable($dataMap->getTableName(), $row['uid']);
948 }
949 return $res;
950 }
951
952 /**
953 * Emits a signal after an object was updated in storage
954 *
955 * @param DomainObjectInterface $object
956 */
957 protected function emitAfterUpdateObjectSignal(DomainObjectInterface $object)
958 {
959 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterUpdateObject', [$object]);
960 }
961
962 /**
963 * Emits a signal after an object was persisted
964 *
965 * @param DomainObjectInterface $object
966 */
967 protected function emitAfterPersistObjectSignal(DomainObjectInterface $object)
968 {
969 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterPersistObject', [$object]);
970 }
971
972 /**
973 * Adds common databse fields to a row
974 *
975 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
976 * @param array &$row
977 */
978 protected function addCommonFieldsToRow(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, array &$row)
979 {
980 $dataMap = $this->dataMapper->getDataMap(get_class($object));
981 $this->addCommonDateFieldsToRow($object, $row);
982 if ($dataMap->getRecordTypeColumnName() !== null && $dataMap->getRecordType() !== null) {
983 $row[$dataMap->getRecordTypeColumnName()] = $dataMap->getRecordType();
984 }
985 if ($object->_isNew() && !isset($row['pid'])) {
986 $row['pid'] = $this->determineStoragePageIdForNewRecord($object);
987 }
988 }
989
990 /**
991 * Adjustes the common date fields of the given row to the current time
992 *
993 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
994 * @param array &$row The row to be updated
995 */
996 protected function addCommonDateFieldsToRow(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, array &$row)
997 {
998 $dataMap = $this->dataMapper->getDataMap(get_class($object));
999 if ($object->_isNew() && $dataMap->getCreationDateColumnName() !== null) {
1000 $row[$dataMap->getCreationDateColumnName()] = $GLOBALS['EXEC_TIME'];
1001 }
1002 if ($dataMap->getModificationDateColumnName() !== null) {
1003 $row[$dataMap->getModificationDateColumnName()] = $GLOBALS['EXEC_TIME'];
1004 }
1005 }
1006
1007 /**
1008 * Iterate over deleted aggregate root objects and process them
1009 */
1010 protected function processDeletedObjects()
1011 {
1012 foreach ($this->deletedEntities as $entity) {
1013 if ($this->session->hasObject($entity)) {
1014 $this->removeEntity($entity);
1015 $this->session->unregisterReconstitutedEntity($entity);
1016 $this->session->unregisterObject($entity);
1017 }
1018 }
1019 $this->deletedEntities = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
1020 }
1021
1022 /**
1023 * Deletes an object
1024 *
1025 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to be removed from the storage
1026 * @param bool $markAsDeleted Whether to just flag the row deleted (default) or really delete it
1027 */
1028 protected function removeEntity(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, $markAsDeleted = true)
1029 {
1030 $dataMap = $this->dataMapper->getDataMap(get_class($object));
1031 $tableName = $dataMap->getTableName();
1032 if ($markAsDeleted === true && $dataMap->getDeletedFlagColumnName() !== null) {
1033 $deletedColumnName = $dataMap->getDeletedFlagColumnName();
1034 $row = [
1035 'uid' => $object->getUid(),
1036 $deletedColumnName => 1
1037 ];
1038 $this->addCommonDateFieldsToRow($object, $row);
1039 $res = $this->storageBackend->updateRow($tableName, $row);
1040 } else {
1041 $res = $this->storageBackend->removeRow($tableName, ['uid' => $object->getUid()]);
1042 }
1043 if ($res === true) {
1044 $this->emitAfterRemoveObjectSignal($object);
1045 }
1046 $this->removeRelatedObjects($object);
1047 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
1048 if ($frameworkConfiguration['persistence']['updateReferenceIndex'] === '1') {
1049 $this->referenceIndex->updateRefIndexTable($tableName, $object->getUid());
1050 }
1051 }
1052
1053 /**
1054 * Emits a signal after an object was removed from storage
1055 *
1056 * @param DomainObjectInterface $object
1057 */
1058 protected function emitAfterRemoveObjectSignal(DomainObjectInterface $object)
1059 {
1060 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterRemoveObject', [$object]);
1061 }
1062
1063 /**
1064 * Remove related objects
1065 *
1066 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The object to scanned for related objects
1067 */
1068 protected function removeRelatedObjects(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object)
1069 {
1070 $className = get_class($object);
1071 $dataMap = $this->dataMapper->getDataMap($className);
1072 $classSchema = $this->reflectionService->getClassSchema($className);
1073 $properties = $object->_getProperties();
1074 foreach ($properties as $propertyName => $propertyValue) {
1075 $columnMap = $dataMap->getColumnMap($propertyName);
1076 if ($columnMap === null) {
1077 continue;
1078 }
1079 $propertyMetaData = $classSchema->getProperty($propertyName);
1080 if ($propertyMetaData['cascade'] === 'remove') {
1081 if ($columnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY) {
1082 foreach ($propertyValue as $containedObject) {
1083 $this->removeEntity($containedObject);
1084 }
1085 } elseif ($propertyValue instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
1086 $this->removeEntity($propertyValue);
1087 }
1088 } elseif ($dataMap->getDeletedFlagColumnName() === null
1089 && $columnMap->getTypeOfRelation() === ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY
1090 ) {
1091 $this->deleteAllRelationsFromRelationtable($object, $propertyName);
1092 }
1093 }
1094 }
1095
1096 /**
1097 * Determine the storage page ID for a given NEW record
1098 *
1099 * This does the following:
1100 * - If the domain object has an accessible property 'pid' (i.e. through a getPid() method), that is used to store the record.
1101 * - If there is a TypoScript configuration "classes.CLASSNAME.newRecordStoragePid", that is used to store new records.
1102 * - If there is no such TypoScript configuration, it uses the first value of The "storagePid" taken for reading records.
1103 *
1104 * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object
1105 * @return int the storage Page ID where the object should be stored
1106 */
1107 protected function determineStoragePageIdForNewRecord(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object = null)
1108 {
1109 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
1110 if ($object !== null) {
1111 if (\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($object, 'pid')) {
1112 $pid = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($object, 'pid');
1113 if (isset($pid)) {
1114 return (int)$pid;
1115 }
1116 }
1117 $className = get_class($object);
1118 if (isset($frameworkConfiguration['persistence']['classes'][$className]) && !empty($frameworkConfiguration['persistence']['classes'][$className]['newRecordStoragePid'])) {
1119 return (int)$frameworkConfiguration['persistence']['classes'][$className]['newRecordStoragePid'];
1120 }
1121 }
1122 $storagePidList = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $frameworkConfiguration['persistence']['storagePid']);
1123 return (int)$storagePidList[0];
1124 }
1125 }