I added a pagination using KNP Paginator. It works well except for one page that render a page for specific ID. This page worked well before I added the pagination. the function was
public function orderDetailAction($id)
For the pagination, I added the Request.
/**
* Display order detail
*
*@Route("/order/{id}", name="requestitem_order")
*@Method("GET")
*/
public function orderDetailAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager()->getRepository('ECAInventoryBundle:RequestItem');
$query = $em->createQueryBuilder('r')
->innerJoin('r.item','i')
->select('i.name')
->addSelect('r.quantity')
->addSelect('i.id')
->addSelect('r.date')
->addSelect('r.remark')
->where('i.id = :ID')->setParameter('ID', $id)
->orderBy('r.date', 'DESC')
->getQuery();
$details = $query->getResult();
$paginator = $this->get('knp_paginator');
$result = $paginator->paginate(
$details,
$request->query->get('page', 1)/*page number*/,
5/*limit per page*/
);
$form = $this->createFormBuilder($details)
->add('id', TextType::class, array('attr' =>array('class' =>'form-control', 'style' =>'margin-bottom:15px')))
->add('name', TextType::class, array('attr' =>array('class' =>'form-control', 'style' =>'margin-bottom:15px')))
->add('quantity', TextType::class, array('attr' =>array('class' =>'form-control', 'style' =>'margin-bottom:15px')))
->add('date', DateType::class, array('attr' =>array('class' =>'form-control', 'style' =>'margin-bottom:15px')))
->add('remark', TextareaType::class, array('attr' =>array('class' =>'form-control', 'style' =>'margin-bottom:15px', 'required' => false),'empty_data' => null))
->getForm();
return $this->render('requestitem/order_details.html.twig', array('details'=> $details, 'form' => $form->createView()));
}
The Twig file is
{% extends 'base.html.twig' %}
{% block body %}
{% set Name = '' %}
{% for detail in details %}
{% set Name = detail.name %}
{% endfor %}
<h1>Detail</h1>
<h2>{{Name}}</h2>
<table class="table table-striped">
<thead>
<tr>
<th scope="row">Quantity</th>
<th scope="row">Date</th>
<th scope="row">Remark</th>
</tr>
</thead>
<tbody>
{% for detail in details %}
<tr>
<td>{{ detail.quantity}}</td>
<td>{{ detail.date|date('d M Y') }}</td>
<td>{{ detail.remark}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{# display navigation #}
<div class="navigation text-center">
{{ knp_pagination_render(details) }}
</div>
<hr />
<a href="{{ path('requestitem_balance')}}" class="btn btn-default">Back to Balance</a>
{% endblock %}
I have the following error
Type error: Argument 2 passed to Knp\Bundle\PaginatorBundle\Twig\Extension\PaginationExtension::render() must be an instance of Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination, array given, called in C:\xampp\htdocs\inventory\var\cache\dev\twig\60\60f10f12ae3f80d30f6ac9425ed3eadb7f6a850a4574537165108f4cd8dfd500.php on line 107
The controller route is @Route("/order/{id}" and the paginator uses this kind of route ?page=2. I don't know if this is the problem.
How can I solve it?
The problem was the route.I changed the route and the function parameters to retrieve the id and page from the url. I make the following changes to make it work.
/**
* Display order detail
*
*@Route("/order/{id}/{page}", name="requestitem_order", defaults={"page": 1})
*@Method("GET")
*/
public function orderDetailAction($id, $page)
{
$em = $this->getDoctrine()->getManager()->getRepository('ECAInventoryBundle:RequestItem');
$query = $em->createQueryBuilder('r')
->innerJoin('r.item','i')
->select('i.name')
->addSelect('r.quantity')
->addSelect('i.id')
->addSelect('r.date')
->addSelect('r.remark')
->where('i.id = :ID')->setParameter('ID', $id)
->orderBy('r.date', 'DESC')
->getQuery();
$details = $query->getResult();
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$details,
$page,
5
);
Instead of using get(page,1) in $pagination, I use $page.
The answer of Paginate in KnpPager not work help me a lot.