sampart/WhiteOctoberPagerfantaBundle

Sorting

kironet opened this issue · 2 comments

Hi,

Want to move from KnpPaginatorBundle and I'm stuck with sorting.
In KnpPaginatorBundle has this
{{ knp_pagination_sortable(products, 'Price ASC', 'p.price', {'direction': 'asc'}) }}

How can I sort in Pagerfanta?
My repository:

    public function getByCategories(array $categories, $page = 1): Pagerfanta
    {
        $qb = $this->createQueryBuilder('p');
        $qb->leftJoin('p.categories', 'c');
        $qb->where('p.enabled = true');
        $qb->andWhere($qb->expr()->in('c.id', $categories));

        return $this->paginator($qb->getQuery()->useQueryCache(true), $page);
    }
    private function paginator(Query $query, int $page): Pagerfanta
    {
        $paginator = new Pagerfanta(new DoctrineORMAdapter($query));
        $paginator->setMaxPerPage(Product::NUM_ITEMS);
        $paginator->setCurrentPage($page);

        return $paginator;
    }

Thanks

Have you tried sorting the response on the query builder? e.g.

$qb->orderBy('p.price', 'ASC');

@kironet Hi. This another bundle can use sorting feature so easily. Try it 👍