Knp Bundle pagination issue
NathalieJ
-
jordane45 Posted messages 30426 Registration date Status Moderator Last intervention -
jordane45 Posted messages 30426 Registration date Status Moderator Last intervention -
Bonjour,
I am trying to implement pagination in my PHP/Symfony project using KNP Bundle. The problem is that while there should only be one post per page, I see all posts on each page.
TacheController :
TacheRepository :
Knp_paginator.yaml:
EDIT : Correction des balises de code. Ajout du LANGAGE pour avoir la COLORATION SYNTAXIQUE.
I am trying to implement pagination in my PHP/Symfony project using KNP Bundle. The problem is that while there should only be one post per page, I see all posts on each page.
TacheController :
@Route("/accueil", name="accueil") public function liste(TacheRepository $tacheRepository, CategorieRepository $categorieRepository, Request $request, PaginatorInterface $paginator): Response { $tache = new Tache(); $form = $this->createForm(FiltreFormType::class, $tache); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { if (!empty($form['q']->getData())) { $tache->setTitre($form['q']->getData()); } if (!empty($form['categories']->getData()->getValues())) { foreach ($form['categories']->getData() as $categorie) { $tache->addCategory($categorie); } } } isset($_POST['datedecroissante']) ? $tri = false : isset($_POST['datecroissante']) ? $tri = true : $tri = false; $taches = $tacheRepository->rechercherTache($tache, $tri); $pagination = $paginator->paginate( $taches, $request->query->getInt('page', 1), 1 ); return $this->render('main/accueil.html.twig', ['taches' => $taches, 'categories' => $categorieRepository->findAll(), 'form' => $form->createView(), 'pagination' => $pagination]); } TacheRepository :
public function rechercherTache(Tache $tache, $tri) { $qb = $this->createQueryBuilder('t') ->select('t', 'c') ->join('t.categories', 'c'); if (!empty($tache->getTitre())) { $qb = $qb->andWhere('t.titre LIKE :q') ->setParameter('q', "%{$tache->getTitre()}%"); } if (!empty($tache->getCategories()->getValues())) { $qb = $qb->andWhere('c.id IN (:categories)') ->setParameter('categories', $tache->getCategories()); } if ($tri == false) { $qb = $qb->orderBy('t.date_creation', 'DESC'); } else { $qb = $qb->orderBy('t.date_creation', 'ASC'); } return $qb->getQuery()->getResult(); } Knp_paginator.yaml:
knp_paginator: page_range: 5 # number of links showed in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links to page 4, 5, 6) default_options: page_name: page # page query parameter name sort_field_name: sort # sort field query parameter name sort_direction_name: direction # sort direction query parameter name distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements filter_field_name: filterField # filter field query parameter name filter_value_name: filterValue # filter value query parameter name template: pagination: '@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig' # sliding pagination controls template sortable: '@KnpPaginator/Pagination/sortable_link.html.twig' # sort link template filtration: '@KnpPaginator/Pagination/filtration.html.twig' # filters template
EDIT : Correction des balises de code. Ajout du LANGAGE pour avoir la COLORATION SYNTAXIQUE.
3 answers
-
Hello,
It seems that you haven't filled in the YAML.
You need to replace the values with what you want, right?
--
.
Best regards,
Jordane -
-
-
- At the controller level, you seem to have set the correct parameters except for the first one...
You need to put the SQL query, not the result of the query$pagination = $paginator->paginate( $query, /* query NOT result */ $request->query->getInt('page', 1), /*page number*/ 1 /*limit per page*/ );
In the documentation, you have an examplepublic function listAction(EntityManagerInterface $em, PaginatorInterface $paginator, Request $request) { $dql = "SELECT a FROM AcmeMainBundle:Article a"; $query = $em->createQuery($dql); $pagination = $paginator->paginate( $query, /* query NOT result */ $request->query->getInt('page', 1), /*page number*/ 10 /*limit per page*/ ); // parameters to template return $this->render('article/list.html.twig', ['pagination' => $pagination]); }
-