Skip to content

Commit

Permalink
Only issue a count once
Browse files Browse the repository at this point in the history
With code like this:

    $query->count();
    ...
    $query->count();

Only issue one count query
  • Loading branch information
AD7six committed Oct 21, 2015
1 parent 2cba8e6 commit ba9c64f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/ORM/Query.php
Expand Up @@ -107,6 +107,15 @@ class Query extends DatabaseQuery implements JsonSerializable, QueryInterface
*/
protected $_beforeFindFired = false;

/**
* The COUNT(*) for the query.
*
* When set, count query execution will be bypassed.
*
* @var int
*/
protected $_resultsCount;

/**
* Constructor
*
Expand Down Expand Up @@ -685,6 +694,10 @@ public function __clone()
*/
public function count()
{
if (isset($this->_resultsCount)) {
return $this->_resultsCount;
}

$query = $this->cleanCopy();
$counter = $this->_counter;
if ($counter) {
Expand Down Expand Up @@ -723,9 +736,10 @@ public function count()
->execute();
}

$result = $statement->fetch('assoc')['count'];
$this->_resultsCount = (int)$statement->fetch('assoc')['count'];
$statement->closeCursor();
return (int)$result;

return $this->_resultsCount;
}

/**
Expand Down

0 comments on commit ba9c64f

Please sign in to comment.