Browse Source

Split the actual count into a seperate method

AD7six 10 years ago
parent
commit
45d3932e04
1 changed files with 17 additions and 6 deletions
  1. 17 6
      src/ORM/Query.php

+ 17 - 6
src/ORM/Query.php

@@ -690,14 +690,26 @@ class Query extends DatabaseQuery implements JsonSerializable, QueryInterface
     /**
      * {@inheritDoc}
      *
-     * Returns the COUNT(*) for the query.
+     * Returns the COUNT(*) for the query. If the query has not been
+     * modified, and the count has already been performed the cached
+     * value is returned
      */
     public function count()
     {
-        if (!$this->_dirty && isset($this->_resultsCount)) {
-            return $this->_resultsCount;
+        if ($this->_dirty || $this->_resultsCount === null) {
+            $this->_resultsCount = $this->_performCount();
         }
 
+        return $this->_resultsCount;
+    }
+
+    /**
+     * Performs and returns the COUNT(*) for the query.
+     *
+     * @return int
+     */
+    protected function _performCount()
+    {
         $query = $this->cleanCopy();
         $counter = $this->_counter;
         if ($counter) {
@@ -736,10 +748,9 @@ class Query extends DatabaseQuery implements JsonSerializable, QueryInterface
                 ->execute();
         }
 
-        $this->_resultsCount = (int)$statement->fetch('assoc')['count'];
+        $result = $statement->fetch('assoc')['count'];
         $statement->closeCursor();
-
-        return $this->_resultsCount;
+        return (int)$result;
     }
 
     /**