[BUGFIX] Prevent sql errors with missing ColumnMap
authorAlexander Schnitzler <alex.schnitzler@typovision.de>
Sat, 17 Nov 2012 16:59:08 +0000 (17:59 +0100)
committerAlexander Schnitzler <typo3@alexanderschnitzler.de>
Sat, 5 Jan 2013 19:40:59 +0000 (20:40 +0100)
If the TCA configuration for an object is missing
no ColumnMap can be generated which leads to sql
errors in method addUnionStatement in Typo3DbBackend.
Instead an Exception should be thrown.

Releases: 6.0, 6.1
Fixes: #7267
Change-Id: Ie364d2199be3d1e0128811e0cc41ff7ad210324b
Reviewed-on: https://review.typo3.org/16536
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Stefan Neufeind
Reviewed-by: Alexander Schnitzler
Tested-by: Alexander Schnitzler
typo3/sysext/extbase/Classes/Persistence/Generic/Exception/MissingColumnMapException.php [new file with mode: 0644]
typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbBackend.php

diff --git a/typo3/sysext/extbase/Classes/Persistence/Generic/Exception/MissingColumnMapException.php b/typo3/sysext/extbase/Classes/Persistence/Generic/Exception/MissingColumnMapException.php
new file mode 100644 (file)
index 0000000..dfa4396
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+namespace TYPO3\CMS\Extbase\Persistence\Generic\Exception;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2010-2012 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
+ *  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.
+ *  A copy is found in the textfile GPL.txt and important notices to the license
+ *  from the author is found in LICENSE.txt distributed with these scripts.
+ *
+ *
+ *  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 "Missing ColumnMap" exception
+ */
+class MissingColumnMapException extends \TYPO3\CMS\Extbase\Persistence\Generic\Exception {
+
+}
+
+?>
\ No newline at end of file
index 5961cb7..b18c339 100644 (file)
@@ -383,19 +383,27 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
         *
         * @param \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object The Value Object
         * @return mixed The matching uid if an object was found, else FALSE
+        *
+        * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\MissingColumnMapException
         */
        public function getUidOfAlreadyPersistedValueObject(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object) {
                $fields = array();
                $parameters = array();
-               $dataMap = $this->dataMapper->getDataMap(get_class($object));
+               $className = get_class($object);
+               $dataMap = $this->dataMapper->getDataMap($className);
+
                $properties = $object->_getProperties();
                foreach ($properties as $propertyName => $propertyValue) {
+                       $columnMap = $dataMap->getColumnMap($propertyName);
+                       if ($columnMap === NULL) {
+                               throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\MissingColumnMapException('The ColumnMap for property "' . $propertyName . '" of class "' . $className . '" is missing.', 1353170711);
+                       }
                        // FIXME We couple the Backend to the Entity implementation (uid, isClone); changes there breaks this method
                        if ($dataMap->isPersistableProperty($propertyName) && $propertyName !== 'uid' && $propertyName !== 'pid' && $propertyName !== 'isClone') {
                                if ($propertyValue === NULL) {
-                                       $fields[] = $dataMap->getColumnMap($propertyName)->getColumnName() . ' IS NULL';
+                                       $fields[] = $columnMap->getColumnName() . ' IS NULL';
                                } else {
-                                       $fields[] = $dataMap->getColumnMap($propertyName)->getColumnName() . '=?';
+                                       $fields[] = $columnMap->getColumnName() . '=?';
                                        $parameters[] = $this->getPlainValue($propertyValue);
                                }
                        }
@@ -548,6 +556,7 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
         * @param array &$sql SQL query parts to add to
         * @param array &$parameters Parameters to bind to the SQL
         * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException
+        * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\MissingColumnMapException
         * @return void
         */
        protected function parseComparison(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ComparisonInterface $comparison, \TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source, array &$sql, array &$parameters) {
@@ -583,6 +592,9 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
                                $columnName = $this->dataMapper->convertPropertyNameToColumnName($propertyName, $className);
                                $dataMap = $this->dataMapper->getDataMap($className);
                                $columnMap = $dataMap->getColumnMap($propertyName);
+                               if ($columnMap === NULL) {
+                                       throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\MissingColumnMapException('The ColumnMap for property "' . $propertyName . '" of class "' . $className . '" is missing.', 1355142273);
+                               }
                                $typeOfRelation = $columnMap instanceof \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap ? $columnMap->getTypeOfRelation() : NULL;
                                if ($typeOfRelation === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
                                        $relationTableName = $columnMap->getRelationTableName();
@@ -687,6 +699,7 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
         * @param array $sql
         * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception
         * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InvalidRelationConfigurationException
+        * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\MissingColumnMapException
         */
        protected function addUnionStatement(&$className, &$tableName, &$propertyPath, array &$sql) {
                $explodedPropertyPath = explode('.', $propertyPath, 2);
@@ -694,6 +707,11 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
                $columnName = $this->dataMapper->convertPropertyNameToColumnName($propertyName, $className);
                $tableName = $this->dataMapper->convertClassNameToTableName($className);
                $columnMap = $this->dataMapper->getDataMap($className)->getColumnMap($propertyName);
+
+               if ($columnMap === NULL) {
+                       throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\MissingColumnMapException('The ColumnMap for property "' . $propertyName . '" of class "' . $className . '" is missing.', 1355142232);
+               }
+
                $parentKeyFieldName = $columnMap->getParentKeyFieldName();
                $childTableName = $columnMap->getChildTableName();
 
@@ -1229,4 +1247,4 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
        }
 }
 
-?>
\ No newline at end of file
+?>