[~BUGFIX] Extbase (Persistence): Fixed some broken MM settings for some IRRE setups...
authorJochen Rau <j.rau@web.de>
Fri, 26 Feb 2010 10:16:00 +0000 (10:16 +0000)
committerJochen Rau <j.rau@web.de>
Fri, 26 Feb 2010 10:16:00 +0000 (10:16 +0000)
typo3/sysext/extbase/Classes/Persistence/Mapper/DataMap.php
typo3/sysext/extbase/Tests/Persistence/Mapper/DataMap_testcase.php

index 8526379..579939d 100644 (file)
@@ -114,7 +114,7 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
        protected function initialize(array $mapping) {
                $this->addCommonColumns();
                $columnConfigurations = array();
-               foreach ($this->getColumnsDefinitions() as $columnName => $columnDefinition) {
+               foreach ($this->getColumnsDefinition() as $columnName => $columnDefinition) {
                        $columnConfigurations[$columnName] = $columnDefinition['config'];
                        $columnConfigurations[$columnName]['mapOnProperty'] = Tx_Extbase_Utility_Extension::convertUnderscoredToLowerCamelCase($columnName);
                }
@@ -133,7 +133,7 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
         * @param string $tableName An optional table name to fetch the columns definition from
         * @return array The TCA columns definition
         */
-       public function getColumnsDefinitions($tableName = '') {
+       public function getColumnsDefinition($tableName = '') {
                $tableName = strlen($tableName) > 0 ? $tableName : $this->getTableName();
                if (TYPO3_MODE === 'FE') {
                        $GLOBALS['TSFE']->includeTCA();
@@ -213,13 +213,13 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
         */
        protected function setRelations(Tx_Extbase_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
                if (isset($columnConfiguration) && $columnConfiguration['type'] !== 'passthrough') {
-                       if (isset($columnConfiguration['foreign_table']) && !isset($columnConfiguration['MM']) && !isset($columnConfiguration['foreign_label'])) {
+                       if (isset($columnConfiguration['foreign_table']) && !isset($columnConfiguration['MM']) && !(isset($columnConfiguration['foreign_label']) || isset($columnConfiguration['foreign_selector']))) {
                                if ($columnConfiguration['maxitems'] == 1) {
                                        $this->setOneToOneRelation($columnMap, $columnConfiguration);
                                } else {
                                        $this->setOneToManyRelation($columnMap, $columnConfiguration);
                                }
-                       } elseif (isset($columnConfiguration['foreign_label']) || isset($columnConfiguration['MM'])) {
+                       } elseif (isset($columnConfiguration['MM']) || (isset($columnConfiguration['foreign_label']) || isset($columnConfiguration['foreign_selector']))) {
                                $this->setManyToManyRelation($columnMap, $columnConfiguration);
                        } else {
                                $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_NONE);
@@ -275,12 +275,13 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
                // TODO support multi table relationships
                $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY);
                if ($columnConfiguration['type'] === 'inline') {
-                       $columns = $this->getColumnsDefinitions($columnConfiguration['foreign_table']);
-                       $columnMap->setChildClassName($this->determineChildClassName($columns[$columnConfiguration['foreign_label']]));
-                       $columnMap->setChildTableName($columns[$columnConfiguration['foreign_label']]['foreign_table']);
+                       $columns = $this->getColumnsDefinition($columnConfiguration['foreign_table']);
+                       $childKeyFieldName = $this->determineChildKeyFieldName($columnConfiguration);
+                       $columnMap->setChildClassName($this->determineChildClassName($columns[$childKeyFieldName]['config']));
+                       $columnMap->setChildTableName($columns[$childKeyFieldName]['config']['foreign_table']);
                        $columnMap->setRelationTableName($columnConfiguration['foreign_table']);
                        $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field']);
-                       $columnMap->setChildKeyFieldName($columnConfiguration['foreign_label']);
+                       $columnMap->setChildKeyFieldName($childKeyFieldName);
                        $columnMap->setChildSortByFieldName($columnConfiguration['foreign_sortby']);
                } else {
                        $columnMap->setChildClassName($this->determineChildClassName($columnConfiguration));
@@ -307,6 +308,24 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
        }
        
        /**
+        * This method returns the foreign key field name in the relation table. For IRRE setups it will return
+        * the foreign_label; if not present the foreign_selector. Default is uid_foreign.
+        *
+        * @param string $columnConfiguration The column configuration of the parent table relation field
+        * @return string The foreign key field name of the relation table
+        */
+       public function determineChildKeyFieldName($columnConfiguration) {
+               if (isset($columnConfiguration['foreign_label'])) {
+                       $childKeyFieldName = $columnConfiguration['foreign_label'];
+               } elseif (isset($columnConfiguration['foreign_selector'])) {
+                       $childKeyFieldName = $columnConfiguration['foreign_selector'];
+               } else {
+                       $childKeyFieldName = 'uid_foreign';
+               }
+               return $childKeyFieldName;
+       }
+       
+       /**
         * 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).
         * 
index cc0e7eb..517ff03 100644 (file)
@@ -300,7 +300,6 @@ class Tx_Extbase_Persistence_Mapper_DataMap_testcase extends Tx_Extbase_BaseTest
                $leftColumnsDefinition = array(
                        'rights' => array(
                                'type' => 'select',
-                               'foreign_class' => 'Tx_MyExtension_RightClass',
                                'foreign_table' => 'tx_myextension_righttable',
                                'foreign_table_where' => 'WHERE 1=1',
                                'MM' => 'tx_myextension_mm',
@@ -309,7 +308,6 @@ class Tx_Extbase_Persistence_Mapper_DataMap_testcase extends Tx_Extbase_BaseTest
                        );
                $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
                $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
-               $mockColumnMap->expects($this->once())->method('setChildClassName')->with($this->equalTo('Tx_MyExtension_RightClass'));
                $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_righttable'));
                $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement')->with($this->equalTo('WHERE 1=1'));
                $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting'));
@@ -329,7 +327,6 @@ class Tx_Extbase_Persistence_Mapper_DataMap_testcase extends Tx_Extbase_BaseTest
                $rightColumnsDefinition = array(
                        'lefts' => array(
                                'type' => 'select',
-                               'foreign_class' => 'Tx_MyExtension_LeftClass',
                                'foreign_table' => 'tx_myextension_lefttable',
                                'MM' => 'tx_myextension_mm',
                                'MM_opposite_field' => 'rights'
@@ -338,7 +335,6 @@ class Tx_Extbase_Persistence_Mapper_DataMap_testcase extends Tx_Extbase_BaseTest
                $leftColumnsDefinition['rights']['MM_opposite_field'] = 'opposite_field';               
                $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
                $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
-               $mockColumnMap->expects($this->once())->method('setChildClassName')->with($this->equalTo('Tx_MyExtension_LeftClass'));
                $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_lefttable'));
                $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement')->with(NULL);
                $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting_foreign'));
@@ -354,6 +350,56 @@ class Tx_Extbase_Persistence_Mapper_DataMap_testcase extends Tx_Extbase_BaseTest
        /**
         * @test
         */
+       public function uid_foreignIsReturnedAsDefaultForChildKeyFieldName() {
+           $leftColumnsDefinition = array(
+                       'rights' => array(
+                               'type' => 'inline',
+                               'foreign_table' => 'tx_myextension_mm',
+                               'foreign_field' => 'uid_local',
+                               )
+                       );
+               $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('dummy'), array(), '', FALSE);
+               $result = $mockDataMap->_callRef('determineChildKeyFieldName', $leftColumnsDefinition['rights']);
+               $this->assertEquals('uid_foreign', $result);
+       }
+       
+       /**
+        * @test
+        */
+       public function theChildKeyFieldNameIsReturnedForForeignLabel() {
+           $leftColumnsDefinition = array(
+                       'rights' => array(
+                               'type' => 'inline',
+                               'foreign_table' => 'tx_myextension_mm',
+                               'foreign_field' => 'uid_local',
+                               'foreign_label' => 'the_child_key_field_name',
+                               )
+                       );
+               $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('dummy'), array(), '', FALSE);
+               $result = $mockDataMap->_callRef('determineChildKeyFieldName', $leftColumnsDefinition['rights']);
+               $this->assertEquals('the_child_key_field_name', $result);
+       }
+       
+       /**
+        * @test
+        */
+       public function theChildKeyFieldNameIsReturnedForForeignSelector() {
+           $leftColumnsDefinition = array(
+                       'rights' => array(
+                               'type' => 'inline',
+                               'foreign_table' => 'tx_myextension_mm',
+                               'foreign_field' => 'uid_local',
+                               'foreign_selector' => 'the_child_key_field_name',
+                               )
+                       );
+               $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('dummy'), array(), '', FALSE);
+               $result = $mockDataMap->_callRef('determineChildKeyFieldName', $leftColumnsDefinition['rights']);
+               $this->assertEquals('the_child_key_field_name', $result);
+       }
+       
+       /**
+        * @test
+        */
        public function columnMapIsInitializedWithManyToManyRelationOfTypeInline() {
            $leftColumnsDefinition = array(
                        'rights' => array(
@@ -366,12 +412,10 @@ class Tx_Extbase_Persistence_Mapper_DataMap_testcase extends Tx_Extbase_BaseTest
                        );
            $relationTableColumnsDefiniton = array(
                        'uid_local' => array(
-                               'foreign_class' => 'Tx_MyExtension_LocalClass',
-                               'foreign_table' => 'tx_myextension_localtable'
+                               'config' => array('foreign_table' => 'tx_myextension_localtable')
                                ),
                        'uid_foreign' => array(
-                               'foreign_class' => 'Tx_MyExtension_RightClass',
-                               'foreign_table' => 'tx_myextension_righttable'
+                               'config' => array('foreign_table' => 'tx_myextension_righttable')
                                )
                        );
            $rightColumnsDefinition = array(
@@ -385,7 +429,6 @@ class Tx_Extbase_Persistence_Mapper_DataMap_testcase extends Tx_Extbase_BaseTest
                        );
                $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
                $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
-               $mockColumnMap->expects($this->once())->method('setChildClassName')->with($this->equalTo('Tx_MyExtension_RightClass'));
                $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_righttable'));
                $mockColumnMap->expects($this->never())->method('setChildTableWhereStatement');
                $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting'));
@@ -396,7 +439,6 @@ class Tx_Extbase_Persistence_Mapper_DataMap_testcase extends Tx_Extbase_BaseTest
                
                $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('getColumnsDefinition', 'determineChildClassName'), array(), '', FALSE);
                $mockDataMap->expects($this->once())->method('getColumnsDefinition')->with($this->equalTo('tx_myextension_mm'))->will($this->returnValue($relationTableColumnsDefiniton));
-               $mockDataMap->expects($this->once())->method('determineChildClassName')->with($this->equalTo($relationTableColumnsDefiniton['uid_foreign']))->will($this->returnValue('Tx_MyExtension_RightClass'));
                $mockDataMap->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
        }