[+BUGFIX] Extbase (Persistence): Fixed a problem with legacy tables where the foreign...
authorJochen Rau <j.rau@web.de>
Wed, 26 Aug 2009 21:41:40 +0000 (21:41 +0000)
committerJochen Rau <j.rau@web.de>
Wed, 26 Aug 2009 21:41:40 +0000 (21:41 +0000)
typo3/sysext/extbase/Classes/Dispatcher.php
typo3/sysext/extbase/Classes/Persistence/Mapper/DataMap.php
typo3/sysext/extbase/Classes/Persistence/Mapper/DataMapper.php

index 4b8ef1c..4dace80 100644 (file)
@@ -119,7 +119,6 @@ class Tx_Extbase_Dispatcher {
         *
         * @return void
         */
-       
        protected function initializeClassLoader() {
                if (!class_exists('Tx_Extbase_Utility_ClassLoader')) {
                        require(t3lib_extmgm::extPath('extbase') . 'Classes/Utility/ClassLoader.php');
index 2471c18..84142b7 100644 (file)
@@ -210,7 +210,7 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
                                } else {
                                        $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY);
                                }
-                               $columnMap->setChildClassName($columnConfiguration['config']['foreign_class']);
+                               $columnMap->setChildClassName($this->determineChildClassName($columnConfiguration));
                                $columnMap->setChildTableName($columnConfiguration['config']['foreign_table']);
                                $columnMap->setChildTableWhereStatement($columnConfiguration['config']['foreign_table_where']);
                                $columnMap->setChildSortbyFieldName($columnConfiguration['config']['foreign_sortby']);
@@ -220,7 +220,7 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
                        } elseif (array_key_exists('MM', $columnConfiguration['config'])) {
                                // TODO support for MM_insert_fields and MM_match_fields
                                $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY);
-                               $columnMap->setChildClassName($columnConfiguration['config']['foreign_class']);
+                               $columnMap->setChildClassName($this->determineChildClassName($columnConfiguration));
                                $columnMap->setChildTableName($columnConfiguration['config']['foreign_table']);
                                $columnMap->setRelationTableName($columnConfiguration['config']['MM']);
                                if (is_array($columnConfiguration['config']['MM_match_fields'])) {
@@ -242,6 +242,31 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
                        }
                }
        }
+       
+       /**
+        * This function determines the child class name. It can either be defined as foreign_class in the column configuration (TCA)
+        * or it must be defined in the extbase framework configuration (reverse mapping from tableName to className).
+        * 
+        * @param $columnConfiguration The column configuration (from TCA)
+        * @return string The class name of the related child object
+        */
+       protected function determineChildClassName($columnConfiguration) {
+               $foreignClassName = '';
+               if (is_string($columnConfiguration['config']['foreign_class']) && (strlen($columnConfiguration['config']['foreign_class']) > 0)) {
+                       $foreignClassName = $columnConfiguration['config']['foreign_class'];
+               } else {
+                       $extbaseSettings = Tx_Extbase_Dispatcher::getExtbaseFrameworkConfiguration();
+                       // TODO Apply a cache to increase performance (profile first)
+                       foreach ($extbaseSettings['persistence']['classes'] as $className => $classConfiguration) {
+                               if ($classConfiguration['mapping']['tableName'] === $columnConfiguration['config']['foreign_table']) {
+                                       $foreignClassName = $className;
+                                       break;
+                               }
+                       }
+               }
+               // TODO Throw exception if no appropriate class name was found
+               return $foreignClassName;
+       }
 
        /**
         * Sets the column maps.
index c67cc6a..3fae18a 100644 (file)
@@ -224,7 +224,7 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                        // TODO Remove dependency to the loading strategy implementation
                        $result = t3lib_div::makeInstance('Tx_Extbase_Persistence_LazyLoadingProxy', $parentObject, $propertyName, $fieldValue, $columnMap);
                } else {
-                       $result = $this->fetchRelatedObjects( $parentObject, $propertyName, $fieldValue, $columnMap);
+                       $result = $this->fetchRelatedObjects($parentObject, $propertyName, $fieldValue, $columnMap);
                }
 
                return $result;
@@ -237,7 +237,6 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
         * @param string $propertyName The name of the proxied property in it's parent
         * @param mixed $fieldValue The raw field value.
         * @param Tx_Extbase_Persistence_Mapper_DataMap $dataMap The corresponding Data Map of the property
-
         */
        public function fetchRelatedObjects(Tx_Extbase_DomainObject_AbstractEntity $parentObject, $propertyName, $fieldValue, Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap) {
                $queryFactory = t3lib_div::makeInstance('Tx_Extbase_Persistence_QueryFactory');
@@ -313,7 +312,7 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
         * @return Tx_Extbase_Persistence_Mapper_DataMap The data map
         */
        public function getDataMap($className) {
-               if (is_string($className) && strlen($className) > 0) {
+               if (!is_string($className) || strlen($className) === 0) throw new Tx_Extbase_Persistence_Exception('No class name was given to retrieve the Data Map for.', 1251315965);
                        if (empty($this->dataMaps[$className])) {
                                // FIXME This is a costy for table name aliases -> implement a DataMapBuilder (knowing the aliases defined in $TCA)
                                $mapping = array();
@@ -326,7 +325,6 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                                                        $tableName = $extbaseSettings['persistence']['classes'][$parentClassName]['mapping']['tableName'];
                                                        break;
                                                }
-                                               // TODO throw Exception
                                        }
                                }
                                if (is_array($extbaseSettings['persistence']['classes'][$parentClassName]['mapping']['columns'])) {
@@ -337,9 +335,6 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                                $this->dataMaps[$className] = $dataMap;
                        }
                        return $this->dataMaps[$className];
-               } else {
-                       return NULL;
-               }
        }
 
        /**