Extbase:
authorJochen Rau <j.rau@web.de>
Tue, 14 Jul 2009 10:04:29 +0000 (10:04 +0000)
committerJochen Rau <j.rau@web.de>
Tue, 14 Jul 2009 10:04:29 +0000 (10:04 +0000)
* Changed Argument: Check for is_object($result) instead of $result !== NULL prevents fatal error; empty result should be catched anyway
* Change: The database handler is now injected to the Typo3DbBackend (for better testability)
* bindVariable() uses now a unique variable name; resolves #3885
* Typo3DbBackend: the variable $sql (and its subparts) are now initialized as arrays (related to #3885 and #3826)
* Typo3DbBackend: Implemented a class variable $useEnableFields (by now only used for better testability)

typo3/sysext/extbase/Classes/Dispatcher.php
typo3/sysext/extbase/Classes/MVC/Controller/Argument.php
typo3/sysext/extbase/Classes/Persistence/Query.php
typo3/sysext/extbase/Classes/Persistence/Storage/Typo3DbBackend.php

index 5309a59..6efdbed 100644 (file)
@@ -56,7 +56,7 @@ class Tx_Extbase_Dispatcher {
                $response = t3lib_div::makeInstance('Tx_Extbase_MVC_Web_Response');
 
                $persistenceSession = t3lib_div::makeInstance('Tx_Extbase_Persistence_Session'); // singleton
-               $storageBackend = t3lib_div::makeInstance('Tx_Extbase_Persistence_Storage_Typo3DbBackend'); // singleton
+               $storageBackend = t3lib_div::makeInstance('Tx_Extbase_Persistence_Storage_Typo3DbBackend', $GLOBALS['TYPO3_DB']); // singleton
                if (isset($configuration['enableAutomaticCacheClearing']) && $configuration['enableAutomaticCacheClearing'] === '1') {
                        $storageBackend->setAutomaticCacheClearing(TRUE);
                } else {
index 5d70dec..24db481 100644 (file)
@@ -305,11 +305,11 @@ class Tx_Extbase_MVC_Controller_Argument {
         */
        protected function findObjectByUid($uid) {
                 $query = $this->queryFactory->create($this->dataType);
-                $object = current($query->matching($query->withUid($uid))->execute());
+                $result = current($query->matching($query->withUid($uid))->execute());
                  // TODO Check if the object is an Aggregate Root (this can be quite difficult because we have no Repository registration 
-                if ($object !== NULL) {
-                       $this->persistenceManager->getSession()->registerReconstitutedObject($object);
-                       return $object;
+                if (is_object($result)) {
+                       $this->persistenceManager->getSession()->registerReconstitutedObject($result);
+                       return $result;
                 } else {
                        return FALSE;
                 }
index 34b7efd..a7b28df 100644 (file)
@@ -276,11 +276,12 @@ class Tx_Extbase_Persistence_Query implements Tx_Extbase_Persistence_QueryInterf
         */
        public function withUid($uid) {
                $sourceSelectorName = $this->getSource()->getSelectorName();
-               $this->operands['uid'] = $uid;
+               $uniqueVariableName = $this->getUniqueVariableName('uid');
+               $this->operands[$uniqueVariableName] = $uid;
                return $this->QOMFactory->comparison(
                        $this->QOMFactory->propertyValue('uid', $sourceSelectorName),
                        Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO,
-                       $this->QOMFactory->bindVariable('uid')
+                       $this->QOMFactory->bindVariable($uniqueVariableName)
                        );
        }
 
@@ -294,6 +295,7 @@ class Tx_Extbase_Persistence_Query implements Tx_Extbase_Persistence_QueryInterf
         */
        public function equals($propertyName, $operand, $caseSensitive = TRUE) {
                $source = $this->getSource();
+               $uniqueVariableName = uniqid($propertyName);
                if ($source instanceof Tx_Extbase_Persistence_QOM_SelectorInterface) {
                        $sourceSelectorName = $this->getSource()->getSelectorName();
                }
@@ -318,16 +320,16 @@ class Tx_Extbase_Persistence_Query implements Tx_Extbase_Persistence_QueryInterf
                        $comparison = $this->QOMFactory->comparison(
                                $this->QOMFactory->propertyValue($propertyName, $sourceSelectorName),
                                Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO,
-                               $this->QOMFactory->bindVariable($propertyName)
+                               $this->QOMFactory->bindVariable($uniqueVariableName)
                                );
 
-                       $this->operands[$propertyName] = $operand;
+                       $this->operands[uniqid($propertyName)] = $operand;
                } else {
                        if ($caseSensitive) {
                                $comparison = $this->QOMFactory->comparison(
                                        $this->QOMFactory->propertyValue($propertyName, $sourceSelectorName),
                                        Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO,
-                                       $this->QOMFactory->bindVariable($propertyName)
+                                       $this->QOMFactory->bindVariable($uniqueVariableName)
                                        );
                        } else {
                                $comparison = $this->QOMFactory->comparison(
@@ -335,14 +337,14 @@ class Tx_Extbase_Persistence_Query implements Tx_Extbase_Persistence_QueryInterf
                                                $this->QOMFactory->propertyValue($propertyName, $sourceSelectorName)
                                        ),
                                        Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO,
-                                       $this->QOMFactory->bindVariable($propertyName)
+                                       $this->QOMFactory->bindVariable($uniqueVariableName)
                                        );
                        }
 
                        if ($caseSensitive) {
-                               $this->operands[$propertyName] = $operand;
+                               $this->operands[$uniqueVariableName] = $operand;
                        } else {
-                               $this->operands[$propertyName] = strtolower($operand);
+                               $this->operands[$uniqueVariableName] = strtolower($operand);
                        }
                }
 
@@ -358,16 +360,17 @@ class Tx_Extbase_Persistence_Query implements Tx_Extbase_Persistence_QueryInterf
         */
        public function like($propertyName, $operand) {
                $source = $this->getSource();
+               $uniqueVariableName = uniqid($propertyName);
                if ($source instanceof Tx_Extbase_Persistence_QOM_SelectorInterface) {
                        $sourceSelectorName = $this->getSource()->getSelectorName();
                }
                // TODO $sourceSelectorName might not be initialized
 
-               $this->operands[$propertyName] = $operand;
+               $this->operands[$uniqueVariableName] = $operand;
                return $this->QOMFactory->comparison(
                        $this->QOMFactory->propertyValue($propertyName, $sourceSelectorName),
                        Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_LIKE,
-                       $this->QOMFactory->bindVariable($propertyName)
+                       $this->QOMFactory->bindVariable($uniqueVariableName)
                        );
        }
 
@@ -380,11 +383,12 @@ class Tx_Extbase_Persistence_Query implements Tx_Extbase_Persistence_QueryInterf
         */
        public function lessThan($propertyName, $operand) {
                $sourceSelectorName = $this->getSource()->getSelectorName();
-               $this->operands[$propertyName] = $operand;
+               $uniqueVariableName = uniqid($propertyName);
+               $this->operands[$uniqueVariableName] = $operand;
                return $this->QOMFactory->comparison(
                        $this->QOMFactory->propertyValue($propertyName, $sourceSelectorName),
                        Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_LESS_THAN,
-                       $this->QOMFactory->bindVariable($propertyName)
+                       $this->QOMFactory->bindVariable($uniqueVariableName)
                        );
        }
 
@@ -397,11 +401,12 @@ class Tx_Extbase_Persistence_Query implements Tx_Extbase_Persistence_QueryInterf
         */
        public function lessThanOrEqual($propertyName, $operand) {
                $sourceSelectorName = $this->getSource()->getSelectorName();
-               $this->operands[$propertyName] = $operand;
+               $uniqueVariableName = uniqid($propertyName);
+               $this->operands[$uniqueVariableName] = $operand;
                return $this->QOMFactory->comparison(
                        $this->QOMFactory->propertyValue($propertyName, $sourceSelectorName),
                        Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_LESS_THAN_OR_EQUAL_TO,
-                       $this->QOMFactory->bindVariable($propertyName)
+                       $this->QOMFactory->bindVariable($uniqueVariableName)
                        );
        }
 
@@ -414,11 +419,12 @@ class Tx_Extbase_Persistence_Query implements Tx_Extbase_Persistence_QueryInterf
         */
        public function greaterThan($propertyName, $operand) {
                $sourceSelectorName = $this->getSource()->getSelectorName();
-               $this->operands[$propertyName] = $operand;
+               $uniqueVariableName = uniqid($propertyName);
+               $this->operands[$uniqueVariableName] = $operand;
                return $this->QOMFactory->comparison(
                        $this->QOMFactory->propertyValue($propertyName, $sourceSelectorName),
                        Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_GREATER_THAN,
-                       $this->QOMFactory->bindVariable($propertyName)
+                       $this->QOMFactory->bindVariable($uniqueVariableName)
                        );
        }
 
@@ -431,13 +437,18 @@ class Tx_Extbase_Persistence_Query implements Tx_Extbase_Persistence_QueryInterf
         */
        public function greaterThanOrEqual($propertyName, $operand) {
                $sourceSelectorName = $this->getSource()->getSelectorName();
-               $this->operands[$propertyName] = $operand;
+               $uniqueVariableName = uniqid($propertyName);
+               $this->operands[$uniqueVariableName] = $operand;
                return $this->QOMFactory->comparison(
                        $this->QOMFactory->propertyValue($propertyName, $sourceSelectorName),
                        Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO,
-                       $this->QOMFactory->bindVariable($propertyName)
+                       $this->QOMFactory->bindVariable($uniqueVariableName)
                        );
        }
 
+       protected function getUniqueVariableName($propertyName) {
+               return uniqid($propertyName);
+       }
+
 }
 ?>
index 0204bae..79b8a60 100644 (file)
@@ -47,7 +47,14 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
         * @var t3lib_pageSelect
         */
        protected $pageSelectObject;
-       
+
+       /**
+        * TRUE if the framework should add the "enable fields" (e.g. checking for hidden or deleted records)
+        *
+        * @var boolean
+        */
+       protected $useEnableFields = TRUE;
+
        /**
         * TRUE if automatic cache clearing in TCEMAIN should be done on insert/update/delete, FALSE otherwise.
         *  
@@ -57,9 +64,11 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
 
        /**
         * Constructs this Storage Backend instance
+        *
+        * @param t3lib_db $databaseHandle The database handle
         */
-       public function __construct() {
-               $this->databaseHandle = $GLOBALS['TYPO3_DB'];
+       public function __construct($databaseHandle) {
+               $this->databaseHandle = $databaseHandle;
        }
        
        /**
@@ -151,6 +160,11 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
         */
        public function getRows(Tx_Extbase_Persistence_QOM_QueryObjectModelInterface $query) {
                $sql = array();
+               $sql['tables'] = array();
+               $sql['fields'] = array();
+               $sql['where'] = array();
+               $sql['enableFields'] = array();
+               $sql['orderings'] = array();
                $parameters = array();
                $tuples = array();
 
@@ -159,8 +173,11 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
 
                $this->parseConstraint($query->getConstraint(), $sql, $parameters, $query->getBoundVariableValues());
                if (!empty($sql['where'])) {
-                       $sqlString .= ' WHERE ' . implode('', $sql['where']) . ' AND ' . implode(' AND ', $sql['enableFields']);
-               } else {
+                       $sqlString .= ' WHERE ' . implode('', $sql['where']);
+                       if (!empty($sql['enableFields'])) {
+                               $sqlString .= ' AND ' . implode(' AND ', $sql['enableFields']);
+                       }
+               } elseif (!empty($sql['enableFields'])) {
                        $sqlString .= ' WHERE ' . implode(' AND ', $sql['enableFields']);
                }
 
@@ -223,7 +240,9 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
                        $sql['fields'][] = $selectorName . '.*';
                        $sql['tables'][] = $selectorName;
                        // TODO Should we make the usage of enableFields configurable? And how? Because the Query object and even the QOM should be abstracted from the storage backend.
-                       $this->addEnableFieldsStatement($selectorName, $sql);
+                       if ($this->useEnableFields === TRUE) {
+                               $this->addEnableFieldsStatement($selectorName, $sql);
+                       }
                } elseif ($source instanceof Tx_Extbase_Persistence_QOM_JoinInterface) {
                        $this->parseJoin($source, $sql, $parameters);
                }