-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
…pager
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Sonata\AdminBundle\Util\TraversableToCollection; | ||
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; | ||
|
||
/** | ||
* @author Lukas Kahwe Smith <[email protected]> | ||
|
@@ -44,16 +45,14 @@ final class SimplePager extends Pager | |
* If set to 3 the pager will generate links to the next three pages | ||
* etc. | ||
*/ | ||
public function __construct( | ||
int $maxPerPage = 10, | ||
private int $threshold = 1 | ||
) { | ||
public function __construct(int $maxPerPage = 10, private int $threshold = 1, private int $total = 0) | ||
{ | ||
parent::__construct($maxPerPage); | ||
} | ||
|
||
public function countResults(): int | ||
{ | ||
return ($this->getPage() - 1) * $this->getMaxPerPage() + ($this->thresholdCount ?? 0); | ||
return $this->total; | ||
} | ||
|
||
public function getCurrentPageResults(): iterable | ||
|
@@ -79,6 +78,23 @@ public function getCurrentPageResults(): iterable | |
return $this->results; | ||
} | ||
|
||
public function getTotalResults(): int | ||
{ | ||
/** @var ProxyQuery $query */ | ||
$query = $this->getQuery(); | ||
Check failure on line 84 in src/Datagrid/SimplePager.php GitHub Actions / PHPStan
Check failure on line 84 in src/Datagrid/SimplePager.php GitHub Actions / PHPStan
Check failure on line 84 in src/Datagrid/SimplePager.php GitHub Actions / PsalmUndefinedDocblockClass
|
||
|
||
if (null === $query) { | ||
throw new \LogicException('Uninitialized query.'); | ||
} | ||
|
||
$query->select(sprintf('COUNT(%s.id)', $query->getRootAlias())); | ||
Check failure on line 90 in src/Datagrid/SimplePager.php GitHub Actions / PHPStan
Check failure on line 90 in src/Datagrid/SimplePager.php GitHub Actions / PHPStan
Check failure on line 90 in src/Datagrid/SimplePager.php GitHub Actions / PsalmUndefinedDocblockClass
Check failure on line 90 in src/Datagrid/SimplePager.php GitHub Actions / PsalmUndefinedDocblockClass
|
||
$query->setFirstResult(0); | ||
Check failure on line 91 in src/Datagrid/SimplePager.php GitHub Actions / PHPStan
Check failure on line 91 in src/Datagrid/SimplePager.php GitHub Actions / PsalmUndefinedDocblockClass
|
||
$query->setMaxResults(1); | ||
Check failure on line 92 in src/Datagrid/SimplePager.php GitHub Actions / PHPStan
Check failure on line 92 in src/Datagrid/SimplePager.php GitHub Actions / PsalmUndefinedDocblockClass
|
||
|
||
|
||
return $query->getDoctrineQuery()->getSingleScalarResult(); | ||
Check failure on line 95 in src/Datagrid/SimplePager.php GitHub Actions / PHPStan
Check failure on line 95 in src/Datagrid/SimplePager.php GitHub Actions / PsalmUndefinedDocblockClass
|
||
} | ||
|
||
/** | ||
* @throws \LogicException the query is uninitialized | ||
*/ | ||
|
@@ -103,6 +119,7 @@ public function init(): void | |
$query->setMaxResults($maxOffset); | ||
|
||
$this->results = $this->getCurrentPageResults(); | ||
$this->total = $this->getTotalResults(); | ||
|
||
$t = (int) ceil(($this->thresholdCount ?? 0) / $this->getMaxPerPage()) + $this->getPage() - 1; | ||
$this->setLastPage(max(1, $t)); | ||
|