[+BUGFIX] Extbase (Persistence): Removed testing code accidentally committed to trunk...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Mapper / DataMapper.php
index f0b2d86..5ca4b3f 100644 (file)
@@ -44,7 +44,7 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
        /**
         * @var Tx_Extbase_Persistence_QOM_QueryObjectModelFactory
         */
-       protected $QOMFactory;
+       protected $QomFactory;
 
        /**
         * @var Tx_Extbase_Persistence_Session
@@ -96,14 +96,13 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
        }
 
        /**
-        * Injects the persistence manager
+        * Injects the persistence session
         *
-        * @param Tx_Extbase_Persistence_ManagerInterface $persistenceManager
+        * @param Tx_Extbase_Persistence_Session $persistenceSession
         * @return void
         */
-       public function injectPersistenceManager(Tx_Extbase_Persistence_ManagerInterface $persistenceManager) {
-               $this->QOMFactory = $persistenceManager->getBackend()->getQOMFactory();
-               $this->persistenceSession = $persistenceManager->getSession();
+       public function injectSession(Tx_Extbase_Persistence_Session $persistenceSession) {
+               $this->persistenceSession = $persistenceSession;
        }
 
        /**
@@ -115,6 +114,16 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
        public function injectReflectionService(Tx_Extbase_Reflection_Service $reflectionService) {
                $this->reflectionService = $reflectionService;
        }
+       
+       /**
+        * Sets the query object model factory
+        *
+        * @param Tx_Extbase_Persistence_QOM_QueryObjectModelFactory $qomFactory
+        * @return void
+        */
+       public function setQomFactory(Tx_Extbase_Persistence_QOM_QueryObjectModelFactory $qomFactory) {
+               $this->qomFactory = $qomFactory;
+       }
 
        /**
         * Maps the (aggregate root) rows and registers them as reconstituted
@@ -181,7 +190,14 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                        $columnMap = $dataMap->getColumnMap($propertyName);
                        $columnName = $columnMap->getColumnName();
                        $propertyValue = NULL;
-                       $propertyType = $columnMap->getPropertyType();
+                       
+                       $propertyMetaData = $this->reflectionService->getClassSchema($className)->getProperty($propertyName);
+                       $propertyType = Tx_Extbase_Persistence_PropertyType::valueFromType($propertyMetaData['type']);
+
+                       if ($propertyType == Tx_Extbase_Persistence_PropertyType::UNDEFINED) {
+                               $propertyType = $columnMap->getPropertyType();
+                       }
+
                        switch ($propertyType) {
                                case Tx_Extbase_Persistence_PropertyType::STRING;
                                case Tx_Extbase_Persistence_PropertyType::DATE;
@@ -194,10 +210,9 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                                }
                                break;
                                case (Tx_Extbase_Persistence_PropertyType::REFERENCE):
-                                       if (is_null($row->getValue($columnName))) {
-                                               $propertyValue = NULL;
-                                       } else {
-                                               $fieldValue = $row->getValue($columnMap->getColumnName());
+                                       $propertyValue = $row->getValue($columnName);
+                                       if (!is_null($propertyValue)) {
+                                               $fieldValue = $row->getValue($columnName);
                                                $result = $this->fetchRelated($object, $propertyName, $fieldValue);
                                                $propertyValue = $this->mapResultToPropertyValue($object, $propertyName, $result);
                                        }
@@ -263,7 +278,11 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                $childSortByFieldName = $columnMap->getChildSortByFieldName();
                if ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_ONE) {
                        $query = $queryFactory->create($this->getType($parentObject, $propertyName));
-                       $query->matching($query->withUid(intval($fieldValue)));
+                       if (isset($parentKeyFieldName)) {
+                               $query->matching($query->equals($parentKeyFieldName, $parentObject->getUid()));
+                       } else {
+                               $query->matching($query->withUid(intval($fieldValue)));
+                       }
                } elseif ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY) {
                        $query = $queryFactory->create($this->getElementType($parentObject, $propertyName));
                        // TODO: This is an ugly hack, just ignoring the storage page state from here. Actually, the query settings would have to be passed into the DataMapper, so we can respect
@@ -283,22 +302,35 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                        // enableFields and storage page settings.
                        $query->getQuerySettings()->setRespectStoragePage(FALSE);
                        $relationTableName = $columnMap->getRelationTableName();
-                       $left = $this->QOMFactory->selector(NULL, $relationTableName);
+                       $left = $this->qomFactory->selector(NULL, $relationTableName);
                        $childClassName = $this->getElementType($parentObject, $propertyName);
                        $childTableName = $columnMap->getChildTableName();
-                       $right = $this->QOMFactory->selector($childClassName, $childTableName);
-                       $joinCondition = $this->QOMFactory->equiJoinCondition($relationTableName, $columnMap->getChildKeyFieldName(), $childTableName, 'uid');
-                       $source = $this->QOMFactory->join(
+                       $right = $this->qomFactory->selector($childClassName, $childTableName);
+                       $joinCondition = $this->qomFactory->equiJoinCondition($relationTableName, $columnMap->getChildKeyFieldName(), $childTableName, 'uid');
+                       $source = $this->qomFactory->join(
                                $left,
                                $right,
                                Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_JOIN_TYPE_INNER,
                                $joinCondition
                                );
+
                        $query->setSource($source);
                        if (!empty($childSortByFieldName)) {
                                $query->setOrderings(array($relationTableName . '.' . $childSortByFieldName => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING));
                        }
-                       $query->matching($query->equals($parentKeyFieldName, $parentObject->getUid()));
+                       
+                       // attempt to support MM_match_fields
+                       $conditions = $query->equals($parentKeyFieldName, $parentObject->getUid());
+
+                       $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
+                       if (count($relationTableMatchFields)) {
+                               foreach($relationTableMatchFields as $relationTableMatchFieldName => $relationTableMatchFieldValue) {
+                                       $relationMatchCondition = $query->equals($relationTableName . '.' . $relationTableMatchFieldName, $relationTableMatchFieldValue);
+                                       $conditions = $query->logicalAnd($conditions, $relationMatchCondition);
+                               }
+                       }
+                       $query->matching($conditions);
+                       
                } else {
                        throw new Tx_Extbase_Persistence_Exception('Could not determine type of relation.', 1252502725);
                }
@@ -459,7 +491,7 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                } else {
                        throw new Tx_Extbase_Persistence_Exception_UnexpectedTypeException('Could not determine the child object object type.', 1251315967);
                }
-               return $elementType;            
+               return $elementType;
        }
 
        /**