[BUGFIX] Always fetch related objects if relation set by child 49/27949/3
authorHelmut Hummel <helmut.hummel@typo3.org>
Sat, 1 Mar 2014 20:41:43 +0000 (21:41 +0100)
committerMarc Bastian Heinrichs <typo3@mbh-software.de>
Mon, 3 Mar 2014 11:21:26 +0000 (12:21 +0100)
If the DataMapper shall map a 1:N relation for
a property of a domain object and the element
type of this property is another domain object
and not a storage type, the data mapper must
always fetch the related elements from persistence
and cannot rely on the persistence session lookup.

This is the case because the relation to the parent
is set on the child side and not the other way around.
The column in the parent row is useless in that case
and does not hold the id of the child.

We can also not rely on the persistence session
because we do not know the identifier of the child
until we fetched it.

Resolves: #56442
Releases: 6.2, 6.1
Change-Id: Icc3ebf9b825f6380691c60839621f01ca9875e4e
Reviewed-on: https://review.typo3.org/27949
Reviewed-by: Xavier Perseguers
Tested-by: Xavier Perseguers
Reviewed-by: Marc Bastian Heinrichs
Tested-by: Marc Bastian Heinrichs
typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapper.php

index 7519b8d..0e43130 100644 (file)
@@ -432,16 +432,20 @@ class DataMapper implements \TYPO3\CMS\Core\SingletonInterface {
         * @see mapResultToPropertyValue()
         */
        protected function mapObjectToClassProperty(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $propertyName, $fieldValue) {
-               if (empty($fieldValue) && !$this->propertyMapsByForeignKey($parentObject, $propertyName)) {
-                       $propertyValue = $this->getEmptyRelationValue($parentObject, $propertyName);
-               } else {
-                       $propertyMetaData = $this->reflectionService->getClassSchema(get_class($parentObject))->getProperty($propertyName);
-
-                       if ($this->persistenceSession->hasIdentifier($fieldValue, $propertyMetaData['type'])) {
-                               $propertyValue = $this->persistenceSession->getObjectByIdentifier($fieldValue, $propertyMetaData['type']);
-                       } else {
+               if ($this->propertyMapsByForeignKey($parentObject, $propertyName)) {
                                $result = $this->fetchRelated($parentObject, $propertyName, $fieldValue);
                                $propertyValue = $this->mapResultToPropertyValue($parentObject, $propertyName, $result);
+               } else {
+                       if ($fieldValue === '') {
+                               $propertyValue = $this->getEmptyRelationValue($parentObject, $propertyName);
+                       } else {
+                               $propertyMetaData = $this->reflectionService->getClassSchema(get_class($parentObject))->getProperty($propertyName);
+                               if ($this->persistenceSession->hasIdentifier($fieldValue, $propertyMetaData['type'])) {
+                                       $propertyValue = $this->persistenceSession->getObjectByIdentifier($fieldValue, $propertyMetaData['type']);
+                               } else {
+                                       $result = $this->fetchRelated($parentObject, $propertyName, $fieldValue);
+                                       $propertyValue = $this->mapResultToPropertyValue($parentObject, $propertyName, $result);
+                               }
                        }
                }