[+TASK] Extbase (Persistence): counting query results does not work with limit constr...
authorBastian Waidelich <bastian@typo3.org>
Wed, 24 Nov 2010 19:51:33 +0000 (19:51 +0000)
committerBastian Waidelich <bastian@typo3.org>
Wed, 24 Nov 2010 19:51:33 +0000 (19:51 +0000)
Tx_Extbase_Persistence_Storage_Typo3DbBackend::getObjectCountByQuery() replaces the SELECT part of a query by COUNT(*) before executing a statement.
This did not work as expected in some cases, e.g. the LIMIT constraint was ignored by the count query.

This resolves: #10956

typo3/sysext/extbase/Classes/Persistence/Storage/Typo3DbBackend.php

index 4b759ce..ced0b0f 100644 (file)
@@ -247,21 +247,31 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
         * Returns the number of tuples matching the query.
         *
         * @param Tx_Extbase_Persistence_QOM_QueryObjectModelInterface $query
-        * @return int The number of matching tuples
+        * @return integer The number of matching tuples
         */
        public function getObjectCountByQuery(Tx_Extbase_Persistence_QueryInterface $query) {
                $constraint = $query->getConstraint();
-               if($constraint instanceof Tx_Extbase_Persistence_QOM_StatementInterface) throw new Tx_Extbase_Persistence_Storage_Exception_BadConstraint('Could not execute count on queries with a constraint of type Tx_Extbase_Persistence_QOM_StatementInterface', 1256661045);
+               if($constraint instanceof Tx_Extbase_Persistence_QOM_StatementInterface) {
+                       throw new Tx_Extbase_Persistence_Storage_Exception_BadConstraint('Could not execute count on queries with a constraint of type Tx_Extbase_Persistence_QOM_StatementInterface', 1256661045);
+               }
                $parameters = array();
                $statementParts = $this->parseQuery($query, $parameters);
-               $statementParts['fields'] = array('COUNT(*)');
-               $statement = $this->buildQuery($statementParts, $parameters);
-               $this->replacePlaceholders($statement, $parameters);
-               // debug($statement,-2);
-               $result = $this->databaseHandle->sql_query($statement);
-               $this->checkSqlErrors($statement);
-               $rows = $this->getRowsFromResult($query->getSource(), $result);
-               return current(current($rows));
+               // if limit is set, we need to count the rows "manually" as COUNT(*) ignores LIMIT constraints
+               if (!empty($statementParts['limit'])) {
+                       $statement = $this->buildQuery($statementParts, $parameters);
+                       $this->replacePlaceholders($statement, $parameters);
+                       $result = $this->databaseHandle->sql_query($statement);
+                       $this->checkSqlErrors($statement);
+                       return $this->databaseHandle->sql_num_rows($result);
+               } else {
+                       $statementParts['fields'] = array('COUNT(*)');
+                       $statement = $this->buildQuery($statementParts, $parameters);
+                       $this->replacePlaceholders($statement, $parameters);
+                       $result = $this->databaseHandle->sql_query($statement);
+                       $this->checkSqlErrors($statement);
+                       $rows = $this->getRowsFromResult($query->getSource(), $result);
+                       return current(current($rows));
+               }
        }
 
        /**