[+TASK] Extbase (Persistence): Whether the relation is multivalue or singlevalue...
authorJochen Rau <j.rau@web.de>
Tue, 25 May 2010 11:10:19 +0000 (11:10 +0000)
committerJochen Rau <j.rau@web.de>
Tue, 25 May 2010 11:10:19 +0000 (11:10 +0000)
typo3/sysext/extbase/Classes/Persistence/Mapper/DataMapFactory.php
typo3/sysext/extbase/Classes/Persistence/Mapper/DataMapper.php
typo3/sysext/extbase/Tests/Persistence/Mapper/DataMapFactory_testcase.php

index 18d6efa..6d54f69 100644 (file)
@@ -101,7 +101,8 @@ class Tx_Extbase_Persistence_Mapper_DataMapFactory {
                        }
                        // if (in_array($propertyName, $classPropertyNames)) { // TODO Enable check for property existance
                                $columnMap = new Tx_Extbase_Persistence_Mapper_ColumnMap($columnName, $propertyName);
-                               $columnMap = $this->setRelations($columnMap, $columnDefinition['config']);
+                               $propertyMetaData = $this->reflectionService->getClassSchema($className)->getProperty($propertyName);
+                               $columnMap = $this->setRelations($columnMap, $columnDefinition['config'], $propertyMetaData);
                                $dataMap->addColumnMap($columnMap);
                        // }
                }
@@ -167,23 +168,26 @@ class Tx_Extbase_Persistence_Mapper_DataMapFactory {
         *
         * @param Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap The column map
         * @param string $columnConfiguration The column configuration from $TCA
+        * @param array $propertyMetaData The property metadata as delivered by the reflection service
         * @return void
         */
-       protected function setRelations(Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap, $columnConfiguration) {
+       protected function setRelations(Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap, $columnConfiguration, $propertyMetaData) {
                if (isset($columnConfiguration) && $columnConfiguration['type'] !== 'passthrough') {
-                       if (isset($columnConfiguration['foreign_table'])) {
+                       if (isset($propertyMetaData['elementType'])) {
                                if (isset($columnConfiguration['MM']) || isset($columnConfiguration['foreign_selector'])) {
                                        $columnMap = $this->setManyToManyRelation($columnMap, $columnConfiguration);
                                } else {
-                                       if (!isset($columnConfiguration['maxitems']) || $columnConfiguration['maxitems'] == 1) {
-                                               $columnMap = $this->setOneToOneRelation($columnMap, $columnConfiguration);
-                                       } else {
-                                               $columnMap = $this->setOneToManyRelation($columnMap, $columnConfiguration);
-                                       }
+                                       $columnMap = $this->setOneToManyRelation($columnMap, $columnConfiguration);
                                }
                        } else {
-                               $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_NONE);
+                               if (isset($propertyMetaData['type']) && strpos($propertyMetaData['type'], '_') !== FALSE) {
+                                       $columnMap = $this->setOneToOneRelation($columnMap, $columnConfiguration);
+                               } else {
+                                       $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_NONE);
+                               }
                        }
+               } else {
+                       $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_NONE);
                }
                return $columnMap;
        }
index 7c01ad5..ef28906 100644 (file)
@@ -432,7 +432,7 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                } else {
                        $propertyMetaData = $this->reflectionService->getClassSchema(get_class($parentObject))->getProperty($propertyName);
                        $columnMap = $this->getDataMap(get_class($parentObject))->getColumnMap($propertyName);
-                       if (in_array($propertyMetaData['type'], array('array', 'ArrayObject', 'Tx_Extbase_Persistence_ObjectStorage'))) {
+                       if (in_array($propertyMetaData['type'], array('array', 'ArrayObject', 'SplObjectStorage', 'Tx_Extbase_Persistence_ObjectStorage'))) {
                                $elementType = $this->getElementType(get_class($parentObject), $propertyName);
                                $objects = array();
                                foreach ($result as $value) {
index 435be4c..0bfe5c7 100644 (file)
@@ -32,97 +32,46 @@ class Tx_Extbase_Persistence_Mapper_DataMapFactory_testcase extends Tx_Extbase_B
        /**
         * @test
         */
-       public function setRelationsDetectsOneToOneRelationOfTypeSelect() {
+       public function setRelationsDetectsOneToOneRelation() {
                $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
            $columnConfiguration = array(
                        'type' => 'select',
                        'foreign_table' => 'tx_myextension_bar',
                        'foreign_field' => 'parentid',
-                       'foreign_table_field' => 'parenttable',
-                       'maxitems' => '1'
                        );
-               $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
-               $mockDataMapFactory->expects($this->once())->method('setOneToOneRelation');
-               $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
-               $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
-               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
-       }
-       
-       /**
-        * @test
-        */
-       public function setRelationsDetectsOneToOneRelationOfTypeSelectWithDefaultMaxitems() {
-               $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
-           $columnConfiguration = array(
-                       'type' => 'select',
-                       'foreign_table' => 'tx_myextension_bar',
-                       'foreign_field' => 'parentid',
-                       'foreign_table_field' => 'parenttable'
+               $propertyMetaData = array(
+                       'type' => 'Tx_Myext_Domain_Model_Foo',
+                       'elementType' => NULL
                        );
                $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
                $mockDataMapFactory->expects($this->once())->method('setOneToOneRelation');
                $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
                $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
-               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
+               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration, $propertyMetaData);
        }
        
        /**
         * @test
         */
-       public function setRelationsDetectsOneToOneRelationOfTypeInline() {
-               $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
-           $columnConfiguration = array(
-                       'type' => 'inline',
-                       'foreign_table' => 'tx_myextension_bar',
-                       'foreign_field' => 'parentid',
-                       'foreign_table_field' => 'parenttable',
-                       'maxitems' => '1'
-                       );
-               $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
-               $mockDataMapFactory->expects($this->once())->method('setOneToOneRelation');
-               $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
-               $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
-               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
-       }
-       
-       /**
-        * @test
-        */
-       public function setRelationsDetectsOneToManyRelationOfTypeSelect() {
+       public function setRelationsDetectsOneToManyRelation() {
                $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
            $columnConfiguration = array(
                        'type' => 'select',
                        'foreign_table' => 'tx_myextension_bar',
                        'foreign_field' => 'parentid',
                        'foreign_table_field' => 'parenttable',
-                       'maxitems' => 9999
                        );
-               $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
-               $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
-               $mockDataMapFactory->expects($this->once())->method('setOneToManyRelation');
-               $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
-               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
-       }
-       
-       /**
-        * @test
-        */
-       public function setRelationsDetectsOneToManyRelationWitTypeInline() {
-               $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
-           $columnConfiguration = array(
-                       'type' => 'inline',
-                       'foreign_table' => 'tx_myextension_bar',
-                       'foreign_field' => 'parentid',
-                       'foreign_table_field' => 'parenttable',
-                       'maxitems' => 9999
+               $propertyMetaData = array(
+                       'type' => 'Tx_Extbase_Persistence_ObjectStorage',
+                       'elementType' => 'Tx_Myext_Domain_Model_Foo'
                        );
                $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
                $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
                $mockDataMapFactory->expects($this->once())->method('setOneToManyRelation');
                $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
-               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
+               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration, $propertyMetaData);
        }
-
+       
        /**
         * @test
         */
@@ -133,11 +82,15 @@ class Tx_Extbase_Persistence_Mapper_DataMapFactory_testcase extends Tx_Extbase_B
                        'foreign_table' => 'tx_myextension_bar',
                        'MM' => 'tx_myextension_mm'
                        );
+               $propertyMetaData = array(
+                       'type' => 'Tx_Extbase_Persistence_ObjectStorage',
+                       'elementType' => 'Tx_Myext_Domain_Model_Foo'
+                       );
                $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
                $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
                $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
                $mockDataMapFactory->expects($this->once())->method('setManyToManyRelation');
-               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
+               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration, $propertyMetaData);
        }
        
        /**
@@ -150,11 +103,15 @@ class Tx_Extbase_Persistence_Mapper_DataMapFactory_testcase extends Tx_Extbase_B
                        'foreign_table' => 'tx_myextension_righttable',
                        'MM' => 'tx_myextension_mm'
                        );
+               $propertyMetaData = array(
+                       'type' => 'Tx_Extbase_Persistence_ObjectStorage',
+                       'elementType' => 'Tx_Myext_Domain_Model_Foo'
+                       );
                $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
                $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
                $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
                $mockDataMapFactory->expects($this->once())->method('setManyToManyRelation');
-               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
+               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration, $propertyMetaData);
        }
        
        /**
@@ -168,11 +125,15 @@ class Tx_Extbase_Persistence_Mapper_DataMapFactory_testcase extends Tx_Extbase_B
                        'foreign_field' => 'uid_local',
                        'foreign_selector' => 'uid_foreign'
                        );
+               $propertyMetaData = array(
+                       'type' => 'Tx_Extbase_Persistence_ObjectStorage',
+                       'elementType' => 'Tx_Myext_Domain_Model_Foo'
+                       );
                $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
                $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
                $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
                $mockDataMapFactory->expects($this->once())->method('setManyToManyRelation');
-               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
+               $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration, $propertyMetaData);
        }
        
        /**