Sorting
kironet opened this issue · 2 comments
kironet commented
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
olorton commented
Have you tried sorting the response on the query builder? e.g.
$qb->orderBy('p.price', 'ASC');
ttskch commented
@kironet Hi. This another bundle can use sorting feature so easily. Try it 👍