whiteoctober / Pagerfanta

Pagination for PHP.
Other
1.59k stars 2 forks source link

I set the initial value of the First Result in query Builder. Why didn't I add Pagerfanta into the Offset when using Pagerfanta #263

Open tb07 opened 5 years ago

tb07 commented 5 years ago
public function getAuthorProducts($uuid)
{
    $queryBuilder = $this->createQueryBuilder('o')
        ->addSelect('author')
        ->innerJoin('o.author', 'author')
        ->andWhere("author.uuid=:uuid")
        ->andWhere("o.enabled=:enabled")
        ->andWhere("o.isShow=:isShow")
        ->setParameter("isShow", true)
        ->setParameter("enabled", true)
        ->setParameter("uuid", $uuid)
        ->setFirstResult(5)
    ;

    return $this->getPaginator($queryBuilder);
}

I print the Paginator image

Query Builder has FirstResult in it

But FirstResult didn't work in the end

I notice that "Pagerfanta/Pagerfanta, "line 316 is not going to fetch the initial FirstResult

private function getCurrentPageResultsFromAdapter()
{
    $offset = $this->calculateOffsetForCurrentPageResults();
    $length = $this->getMaxPerPage();

    return $this->adapter->getSlice($offset, $length);
}

private function calculateOffsetForCurrentPageResults()
{
    return ($this->getCurrentPage() - 1) * $this->getMaxPerPage();
}
sampart commented 5 years ago

Hi @tangbingbing, thanks for getting involved.

The DoctrineORM adapter, which I think is the one you're using, actually uses setFirstResult itself behind the scenes, so I guess that's why your setting of it is being ignored.

If we wanted to support starting from an arbitrary offset, we'd need to modify the adapter method and get people to use a new method on Pagerfanta to set start point rather than doing it directly in the querybuilder, I think.

Anyway, regardless of exactly how we do it, this is quite a major change, I'm afraid. Could you explain more about your use case here? That'll help us work out whether it's worth suggesting you make a Pull Request for this change or whether it's not worth incorporating.