Skip to content

Commit

Permalink
Merge pull request #21 from jvasseur/lazy-orm-query-result
Browse files Browse the repository at this point in the history
Make ORMQueryResult lazy
  • Loading branch information
beberlei committed Feb 17, 2023
2 parents 9518194 + 11e0156 commit 5288d6d
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/Porpaginas/Doctrine/ORM/ORMQueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class ORMQueryResult implements Result
*/
private $result;

/**
* @var int|null
*/
private $count;

public function __construct($query, $fetchCollection = true)
{
if ($query instanceof QueryBuilder) {
Expand Down Expand Up @@ -82,9 +87,11 @@ public function take($offset, $limit)
*/
public function count()
{
$this->loadQuery();
if ($this->count === null) {
$this->count = count(new Paginator($this->query, $this->fetchCollection));
}

return count($this->result);
return $this->count;
}

/**
Expand All @@ -93,16 +100,12 @@ public function count()
* @return Iterator
*/
public function getIterator()
{
$this->loadQuery();

return new ArrayIterator($this->result);
}

private function loadQuery()
{
if ($this->result === null) {
$this->result = $this->query->getResult();
$this->count = count($this->result);
}

return new ArrayIterator($this->result);
}
}

0 comments on commit 5288d6d

Please sign in to comment.