EXTMVC:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Mapper / ObjectRelationalMapper.php
index abddf07..404eeaf 100644 (file)
@@ -215,10 +215,9 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
         * @return void
         */
        // SK: I need to check this more thorougly
-       protected function insertObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object, $parentObject = NULL, $parentPropertyName = NULL) {
+       protected function insertObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object, $parentObject = NULL, $parentPropertyName = NULL, $recurseIntoRelations = TRUE) {
                $properties = $object->_getProperties();
                $dataMap = $this->getDataMap(get_class($object));
-               $relations = $this->getRelations($dataMap, $properties);
                $row = $this->getRow($dataMap, $properties);
 
                if ($parentObject instanceof TX_EXTMVC_DomainObject_AbstractDomainObject && $parentPropertyName !== NULL) {
@@ -235,6 +234,7 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
                }
 
                unset($row['uid']);
+
                $row['pid'] = !empty($this->cObj->data['pages']) ? $this->cObj->data['pages'] : $GLOBALS['TSFE']->id;
                $row['tstamp'] = time();
 
@@ -245,10 +245,7 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
                        );
                $object->_reconstituteProperty('uid', $GLOBALS['TYPO3_DB']->sql_insert_id());
 
-               $recurseIntoRelations = TRUE; // TODO Make this configurable
-               if ($recurseIntoRelations === TRUE) {
-                       $this->persistRelations($object, $propertyName, $relations);
-               }
+               $this->persistRelations($object, $propertyName, $this->getRelations($dataMap, $properties));
        }
 
        /**
@@ -257,12 +254,12 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
         * @return void
         */
        // SK: I need to check this more thorougly
-       protected function updateObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object, $parentObject = NULL, $parentPropertyName = NULL) {
+       protected function updateObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object, $parentObject = NULL, $parentPropertyName = NULL, $recurseIntoRelations = TRUE) {
                $properties = $object->_getDirtyProperties();
                $dataMap = $this->getDataMap(get_class($object));
-               $relations = $this->getRelations($dataMap, $properties);
                $row = $this->getRow($dataMap, $properties);
                unset($row['uid']);
+               // TODO Check for crdate column
                $row['crdate'] = time();
                if (!empty($GLOBALS['TSFE']->fe_user->user['uid'])) {
                        $row['cruser_id'] = $GLOBALS['TSFE']->fe_user->user['uid'];
@@ -287,10 +284,7 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
                        $row
                        );
 
-               $recurseIntoRelations = TRUE; // TODO make parametric
-               if ($recurseIntoRelations === TRUE) {
-                       $this->persistRelations($object, $propertyName, $relations);
-               }
+               $this->persistRelations($object, $propertyName, $this->getRelations($dataMap, $properties));
        }
 
        /**
@@ -329,7 +323,7 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
         * Returns a table row to be inserted or updated in the database
         *
         * @param TX_EXTMVC_Persistence_Mapper_DataMap $dataMap The appropriate data map representing a database table
-        * @param string $properties The properties of the object
+        * @param array $properties The properties of the object
         * @return array A single row to be inserted in the database
         */
        // SK: I need to check this more thorougly
@@ -341,6 +335,7 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
                        if ($columnMap->getTypeOfRelation() === TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY) {
                                $row[$columnName] = count($properties[$propertyName]);
                        } elseif ($columnMap->getTypeOfRelation() === TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
+                               // TODO Check if this elseif is needed or could be merged with the lines above
                                $row[$columnName] = count($properties[$propertyName]);
                        } else {
                                if ($properties[$propertyName] !== NULL) {
@@ -371,7 +366,7 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
        }
 
        /**
-        * Inserts and updates all relations of an object. It also updates relation tables.
+        * Inserts and updates all relations of an object. It also inserts and updates data in relation tables.
         *
         * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object The object for which the relations should be updated
         * @param string $propertyName The name of the property holding the related child objects
@@ -394,14 +389,11 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
                                        }
                                }
                        }
-                       if ($typeOfRelation === TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
-                               $this->updateRelationsInRelationTable($relatedObjects, $object, $propertyName);
-                       }
                }
        }
 
        /**
-        * Deletes all relations of an object. It also updates relation tables.
+        * Deletes all relations of an object.
         *
         * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object The object for which the relations should be updated
         * @param string $propertyName The name of the property holding the related child objects
@@ -415,7 +407,7 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
                                foreach ($relatedObjects as $relatedObject) {
                                        $this->deleteObject($relatedObject, $object, $propertyName);
                                        if ($dataMap->getColumnMap($propertyName)->getTypeOfRelation() === TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
-                                               $this->deleteRelation($relatedObject, $object, $propertyName);
+                                               $this->deleteRelationInRelationTable($relatedObject, $object, $propertyName);
                                        }
                                }
                        }
@@ -439,13 +431,10 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
                        'sorting' => 9999 // TODO sorting of mm table items
                        );
                $tableName = $dataMap->getColumnMap($parentPropertyName)->getRelationTableName();
-               // debug($rowToInsert, $tableName);
                $res = $GLOBALS['TYPO3_DB']->exec_INSERTquery(
                        $tableName,
                        $rowToInsert
                        );
-               // var_dump($res);
-               // debug(mysql_error());
        }
 
        /**
@@ -456,7 +445,7 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
         * @param string $parentPropertyName The name of the parent object's property where the related objects are stored in
         * @return void
         */
-       protected function updateRelationsInRelationTable($relatedObjects, TX_EXTMVC_DomainObject_AbstractDomainObject $parentObject, $parentPropertyName) {
+       protected function deleteRelationInRelationTable($relatedObject, TX_EXTMVC_DomainObject_AbstractDomainObject $parentObject, $parentPropertyName) {
                $dataMap = $this->getDataMap(get_class($parentObject));
                $tableName = $dataMap->getColumnMap($parentPropertyName)->getRelationTableName();
                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
@@ -469,8 +458,8 @@ class TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper implements t3lib_Singl
                        $existingRelations[current($row)] = current($row);
                }
                $relationsToDelete = $existingRelations;
-               if (is_array($relatedObjects)) {
-                       foreach ($relatedObjects as $relatedObject) {
+               if (is_array($relatedObject)) {
+                       foreach ($relatedObject as $relatedObject) {
                                $relatedObjectUid = $relatedObject->getUid();
                                if (array_key_exists($relatedObjectUid, $relationsToDelete)) {
                                        unset($relationsToDelete[$relatedObjectUid]);