[+FEATURE] Extbase (Persistence): Implemented support for mm_opposite_field now....
authorJochen Rau <j.rau@web.de>
Wed, 22 Jul 2009 08:54:21 +0000 (08:54 +0000)
committerJochen Rau <j.rau@web.de>
Wed, 22 Jul 2009 08:54:21 +0000 (08:54 +0000)
typo3/sysext/extbase/Classes/Persistence/Backend.php
typo3/sysext/extbase/Classes/Persistence/LazyLoadingProxy.php
typo3/sysext/extbase/Classes/Persistence/Mapper/ColumnMap.php
typo3/sysext/extbase/Classes/Persistence/Mapper/DataMap.php
typo3/sysext/extbase/Classes/Persistence/Mapper/DataMapper.php
typo3/sysext/extbase/Classes/Persistence/Storage/Typo3DbBackend.php

index d28df9d..1366599 100644 (file)
@@ -395,15 +395,15 @@ class Tx_Extbase_Persistence_Backend implements Tx_Extbase_Persistence_BackendIn
         */
        protected function insertRelation(Tx_Extbase_DomainObject_DomainObjectInterface $relatedObject, Tx_Extbase_DomainObject_DomainObjectInterface $parentObject, $parentPropertyName) {
                $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
+               $columnMap = $dataMap->getColumnMap($parentPropertyName);
                $row = array(
-                       'uid_local' => (int)$parentObject->getUid(), // TODO Aliases for relation field names
-                       'uid_foreign' => (int)$relatedObject->getUid(),
+                       $columnMap->getParentKeyFieldName() => (int)$parentObject->getUid(),
+                       $columnMap->getChildKeyFieldName() => (int)$relatedObject->getUid(),
                        'tablenames' => $dataMap->getTableName(),
                        'sorting' => 9999 // TODO sorting of mm table items
                        );
-               $tableName = $dataMap->getColumnMap($parentPropertyName)->getRelationTableName();
                $res = $this->storageBackend->addRow(
-                       $tableName,
+                       $columnMap->getRelationTableName(),
                        $row,
                        TRUE);
                return $res;
@@ -577,12 +577,12 @@ class Tx_Extbase_Persistence_Backend implements Tx_Extbase_Persistence_BackendIn
         */
        protected function deleteRelationInRelationTable($relatedObject, Tx_Extbase_DomainObject_DomainObjectInterface $parentObject, $parentPropertyName) {
                $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
-               $tableName = $dataMap->getColumnMap($parentPropertyName)->getRelationTableName();
+               $columnMap = $dataMap->getColumnMap($parentPropertyName);
                // TODO Remove dependency to the t3lib_db instance
                $res = $this->persistenceBackend->exec_SELECTquery(
-                       'uid_foreign',
+                       $columnMap->getChildKeyFieldName(),
                        $tableName,
-                       'uid_local=' . $parentObject->getUid()
+                       $columnMap->getParentKeyFieldName() . $parentObject->getUid()
                        );
                $existingRelations = array();
                while($row = $this->persistenceBackend->sql_fetch_assoc($res)) {
@@ -600,8 +600,8 @@ class Tx_Extbase_Persistence_Backend implements Tx_Extbase_Persistence_BackendIn
                if (count($relationsToDelete) > 0) {
                        $relationsToDeleteList = implode(',', $relationsToDelete);
                        $res = $this->persistenceBackend->exec_DELETEquery(
-                               $tableName,
-                               'uid_local=' . $parentObject->getUid() . ' AND uid_foreign IN (' . $relationsToDeleteList . ')'
+                               $columnMap->getRelationTableName(),
+                               $columnMap->getParentKeyFieldName() . '=' . $parentObject->getUid() . ' AND ' . $columnMap->getChildKeyFieldName() . ' IN (' . $relationsToDeleteList . ')'
                                );
                }
        }
index 3fcc055..0904c77 100644 (file)
@@ -1,29 +1,29 @@
 <?php
 /***************************************************************
-*  Copyright notice
-*
-*  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
-*  All rights reserved
-*
-*  This class is a backport of the corresponding class of FLOW3.
-*  All credits go to the v5 team.
-*
-*  This script is part of the TYPO3 project. The TYPO3 project is
-*  free software; you can redistribute it and/or modify
-*  it under the terms of the GNU General Public License as published by
-*  the Free Software Foundation; either version 2 of the License, or
-*  (at your option) any later version.
-*
-*  The GNU General Public License can be found at
-*  http://www.gnu.org/copyleft/gpl.html.
-*
-*  This script is distributed in the hope that it will be useful,
-*  but WITHOUT ANY WARRANTY; without even the implied warranty of
-*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-*  GNU General Public License for more details.
-*
-*  This copyright notice MUST APPEAR in all copies of the script!
-***************************************************************/
+ *  Copyright notice
+ *
+ *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
+ *  All rights reserved
+ *
+ *  This class is a backport of the corresponding class of FLOW3.
+ *  All credits go to the v5 team.
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
 
 /**
  * A proxy that can replace any object and replaces itself in it's parent on
@@ -33,7 +33,7 @@
  * @subpackage Persistence
  * @version $Id: LazyLoadingProxy.php 2591 2009-06-09 19:23:47Z k-fish $
  */
- // TODO Implement support for CountableInterface
+// TODO Implement support for CountableInterface
 class Tx_Extbase_Persistence_LazyLoadingProxy {
 
        /**
@@ -104,16 +104,16 @@ class Tx_Extbase_Persistence_LazyLoadingProxy {
                        $left = $this->QOMFactory->selector($relationTableName);
                        $childTableName = $columnMap->getChildTableName();
                        $right = $this->QOMFactory->selector($childTableName);
-                       $joinCondition = $this->QOMFactory->equiJoinCondition($relationTableName, 'uid_foreign', $childTableName, 'uid');
+                       $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
-                               );
+                       $left,
+                       $right,
+                       Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_JOIN_TYPE_INNER,
+                       $joinCondition
+                       );
                        $query = $this->queryFactory->create($columnMap->getChildClassName());
                        $query->setSource($source);
-                       $objects = $query->matching($query->equals('uid_local', $this->parentObject->getUid()))->execute();
+                       $objects = $query->matching($query->equals($columnMap->getChildKeyFieldName(), $this->parentObject->getUid()))->execute();
                        foreach ($objects as $object) {
                                $objectStorage->attach($object);
                        }
index 18187e8..555f47f 100644 (file)
@@ -124,6 +124,13 @@ class Tx_Extbase_Persistence_Mapper_ColumnMap {
        protected $parentTableFieldName;
 
        /**
+        * The name of the field holding the children key
+        *
+        * @var string
+        **/
+       protected $childrenKeyFieldName;
+
+       /**
         * Constructs a Column Map
         * 
         * @param string $columnName The column name
@@ -266,6 +273,14 @@ class Tx_Extbase_Persistence_Mapper_ColumnMap {
        public function getParentTableFieldName() {
                return $this->parentTableFieldName;
        }
+       
+       public function setChildKeyFieldName($childKeyFieldName) {
+               $this->childKeyFieldName = $childKeyFieldName;
+       }
+
+       public function getChildKeyFieldName() {
+               return $this->childKeyFieldName;
+       }
 
 }
 ?>
\ No newline at end of file
index a148dba..cb09227 100644 (file)
@@ -1,26 +1,26 @@
 <?php
 /***************************************************************
-*  Copyright notice
-*
-*  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
-*  All rights reserved
-*
-*  This script is part of the TYPO3 project. The TYPO3 project is
-*  free software; you can redistribute it and/or modify
-*  it under the terms of the GNU General Public License as published by
-*  the Free Software Foundation; either version 2 of the License, or
-*  (at your option) any later version.
-*
-*  The GNU General Public License can be found at
-*  http://www.gnu.org/copyleft/gpl.html.
-*
-*  This script is distributed in the hope that it will be useful,
-*  but WITHOUT ANY WARRANTY; without even the implied warranty of
-*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-*  GNU General Public License for more details.
-*
-*  This copyright notice MUST APPEAR in all copies of the script!
-***************************************************************/
+ *  Copyright notice
+ *
+ *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
 
 /**
  * A data map to map a single table configured in $TCA on a domain object.
@@ -56,7 +56,7 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
         *
         * @param string $className The class name. This determines the table to fetch the configuration for
         */
-       // TODO Refactor to factory pattern (DataMapFactory) and value object (DataMap)  
+       // TODO Refactor to factory pattern (DataMapFactory) and value object (DataMap)
        public function __construct($className, $tableName = '', array $mapping = array()) {
                $this->setClassName($className);
                if (empty($tableName)) {
@@ -102,10 +102,10 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
        public function getTableName() {
                return $this->tableName;
        }
-       
+
        /**
-        * Initializes the data map by adding column maps for all the configured columns in the $TCA. 
-        * It also resolves the type of values the column is holding and the typo of relation the column 
+        * Initializes the data map by adding column maps for all the configured columns in the $TCA.
+        * It also resolves the type of values the column is holding and the typo of relation the column
         * represents.
         *
         * @return void
@@ -124,13 +124,12 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
                                $columnMap = new Tx_Extbase_Persistence_Mapper_ColumnMap($columnName, $propertyName);
                                $this->setPropertyType($columnMap, $columnConfiguration);
                                // TODO Check support for IRRE
-                               // TODO support for MM_insert_fields and MM_match_fields
                                $this->setRelations($columnMap, $columnConfiguration);
                                $this->addColumnMap($columnMap);
                        }
                }
        }
-       
+
        /**
         * Adds available common columns (e.g. tstamp or crdate) to the data map. It takes the configured column names
         * into account.
@@ -193,7 +192,7 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
        }
 
        /**
-        * This method tries to determine the type of type of relation to other tables and sets it based on 
+        * This method tries to determine the type of type of relation to other tables and sets it based on
         * the $TCA column configuration
         *
         * @param string $columnMap The column map
@@ -219,12 +218,22 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
                                $columnMap->setParentKeyFieldName($columnConfiguration['config']['foreign_field']);
                                $columnMap->setParentTableFieldName($columnConfiguration['config']['foreign_table_field']);
                        }
-                       //                      TODO Support MM_match_fields
                } 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->setChildTableName($columnConfiguration['config']['foreign_table']);
                        $columnMap->setRelationTableName($columnConfiguration['config']['MM']);
+                       // TODO We currently do not support multi table relationships
+                       if ($columnConfiguration['config']['MM_opposite_field']) {      // in case of a reverse relation
+                               $columnMap->setParentKeyFieldName('uid_foreign');
+                               $columnMap->setChildKeyFieldName('uid_local');
+                               $columnMap->setChildSortByFieldName('sorting_foreign');
+                       } else {
+                               $columnMap->setParentKeyFieldName('uid_local');
+                               $columnMap->setChildKeyFieldName('uid_foreign');
+                               $columnMap->setChildSortByFieldName('sorting');
+                       }
                } else {
                        $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_NONE);
                }
@@ -243,7 +252,7 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
        /**
         * Adds a given column map to the data map.
         *
-        * @param Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap The column map 
+        * @param Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap The column map
         * @return void
         */
        public function addColumnMap(Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap) {
@@ -251,7 +260,7 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
        }
 
        /**
-        * Builds a column map out of the given column name, type of value (optional), and type of 
+        * Builds a column map out of the given column name, type of value (optional), and type of
         * relation (optional) and adds it to the data map.
         *
         * @param string $columnName The column name
@@ -264,7 +273,7 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
                if (empty($propertyName)) {
                        $propertyName = t3lib_div::underscoredToLowerCamelCase($columnName);
                }
-               
+
                $columnMap = new Tx_Extbase_Persistence_Mapper_ColumnMap($columnName, $propertyName);
                $columnMap->setPropertyType($propertyType);
                $columnMap->setTypeOfRelation($typeOfRelation);
@@ -284,7 +293,7 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
        /**
         * Returns the column map corresponding to the given property name.
         *
-        * @param string $propertyName 
+        * @param string $propertyName
         * @return Tx_Extbase_Persistence_Mapper_ColumnMap|NULL The column map or NULL if no corresponding column map was found.
         */
        public function getColumnMap($propertyName) {
@@ -300,7 +309,7 @@ class Tx_Extbase_Persistence_Mapper_DataMap {
        public function isPersistableProperty($propertyName) {
                return isset($this->columnMaps[$propertyName]);
        }
-       
+
        /**
         * Check if versioning is enabled .
         *
index 77ea041..11c63bd 100644 (file)
@@ -234,7 +234,7 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                                $left = $this->QOMFactory->selector($relationTableName);
                                $childTableName = $columnMap->getChildTableName();
                                $right = $this->QOMFactory->selector($childTableName);
-                               $joinCondition = $this->QOMFactory->equiJoinCondition($relationTableName, 'uid_foreign', $childTableName, 'uid');
+                               $joinCondition = $this->QOMFactory->equiJoinCondition($relationTableName, $columnMap->getChildKeyFieldName(), $childTableName, 'uid');
                                $source = $this->QOMFactory->join(
                                        $left,
                                        $right,
@@ -243,7 +243,7 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                                        );
                                $query = $this->queryFactory->create($columnMap->getChildClassName());
                                $query->setSource($source);
-                               $objects = $query->matching($query->equals('uid_local', $parentObject->getUid()))->execute();
+                               $objects = $query->matching($query->equals($columnMap->getParentKeyFieldName(), $parentObject->getUid()))->execute();
                                foreach ($objects as $object) {
                                        $objectStorage->attach($object);
                                }
index b9233f9..520f4bf 100644 (file)
@@ -462,7 +462,7 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
        protected function addEnableFieldsStatement($selectorName, array &$sql) {
                // TODO We have to call the appropriate API method if we are in TYPO3BE mode
                if (is_array($GLOBALS['TCA'][$selectorName]['ctrl'])) {
-                       $statement = substr($GLOBALS['TSFE']->sys_page->enableFields($selectorName), 4);
+                       $statement = substr($GLOBALS['TSFE']->sys_page->enableFields($selectorName), 5);
                        if(!empty($statement)) {
                                $sql['enableFields'][] = $statement;
                        }