Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle SonataAdminBundle Pager::nbresults deprecation #501

Merged
merged 2 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"doctrine/mongodb-odm": "^2.1",
"doctrine/mongodb-odm-bundle": "^4.0",
"doctrine/persistence": "^1.3.4 || ^2.0",
"sonata-project/admin-bundle": "^3.84",
"sonata-project/admin-bundle": "^3.86",
"sonata-project/form-extensions": "^0.1 || ^1.4",
"symfony/config": "^4.4",
"symfony/dependency-injection": "^4.4",
Expand Down
103 changes: 95 additions & 8 deletions src/Datagrid/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,71 @@ class Pager extends BasePager
{
protected $queryBuilder = null;

public function computeNbResult()
/**
* @var int
*/
private $resultsCount = 0;

public function countResults(): int
{
$countQuery = clone $this->getQuery();
// NEXT_MAJOR: just return "$this->resultsCount" directly.
$deprecatedCount = $this->getNbResults('sonata_deprecation_mute');

if (\count($this->getParameters()) > 0) {
$countQuery->setParameters($this->getParameters());
if ($deprecatedCount === $this->resultsCount) {
return $this->resultsCount;
}

@trigger_error(sprintf(
'Relying on the protected property "%s::$nbResults" and its getter/setter is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will fail 4.0. Use "countResults()" and "setResultsCount()" instead.',
self::class,
), E_USER_DEPRECATED);

return $deprecatedCount;
}

public function setResultsCount(int $count): void
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
{
$this->resultsCount = $count;
// NEXT_MAJOR: Remove this line.
$this->setNbResults($count, 'sonata_deprecation_mute');
}

/**
* NEXT_MAJOR: remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x
*
* @return int
*/
public function getNbResults()
{
if ('sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) {
@trigger_error(sprintf(
'The %s() method is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in 4.0. Use "countResults()" instead.',
__METHOD__,
), E_USER_DEPRECATED);
}

return $this->nbResults;
}

/**
* NEXT_MAJOR: remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x
*
* @return int
*/
public function computeNbResult()
{
if ('sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) {
@trigger_error(sprintf(
'The %s() method is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in 4.0.',
__METHOD__,
), E_USER_DEPRECATED);
}

return $countQuery->count()->getQuery()->execute();
return $this->computeResultsCount();
}

public function getResults()
Expand All @@ -50,7 +106,7 @@ public function init()
{
$this->resetIterator();

$this->setNbResults($this->computeNbResult());
$this->setResultsCount($this->computeNbResult('sonata_deprecation_mute'));
franmomu marked this conversation as resolved.
Show resolved Hide resolved

$this->getQuery()->setFirstResult(0);
$this->getQuery()->setMaxResults(0);
Expand All @@ -59,15 +115,46 @@ public function init()
$this->getQuery()->setParameters($this->getParameters());
}

if (0 === $this->getPage() || 0 === $this->getMaxPerPage() || 0 === $this->getNbResults()) {
if (0 === $this->getPage() || 0 === $this->getMaxPerPage() || 0 === $this->countResults()) {
$this->setLastPage(0);
} else {
$offset = ($this->getPage() - 1) * $this->getMaxPerPage();

$this->setLastPage((int) ceil($this->getNbResults() / $this->getMaxPerPage()));
$this->setLastPage((int) ceil($this->countResults() / $this->getMaxPerPage()));

$this->getQuery()->setFirstResult($offset);
$this->getQuery()->setMaxResults($this->getMaxPerPage());
}
}

/**
* NEXT_MAJOR: remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x
*
* @param int $nb
*/
protected function setNbResults($nb)
{
if ('sonata_deprecation_mute' !== (\func_get_args()[1] ?? null)) {
@trigger_error(sprintf(
'The %s() method is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in 4.0. Use "setResultsCount()" instead.',
__METHOD__,
), E_USER_DEPRECATED);
}

$this->nbResults = $nb;
$this->resultsCount = (int) $nb;
}

private function computeResultsCount(): int
{
$countQuery = clone $this->getQuery();

if (\count($this->getParameters()) > 0) {
$countQuery->setParameters($this->getParameters());
}

return (int) $countQuery->count()->getQuery()->execute();
}
}