我在让 Knp 分页器前进到下一页时遇到问题。如图所示,页面导航栏正确显示(名称是假的),并且排序正常。但是,当我尝试前进到第 2 页时,视图仍停留在第 1 页,即使 URL 现在看起来像这样:my.page/show?page=2
视图模板由 AttendeeController 调用,嵌入在show.html.twig中:
<div class="attendance_table">
{{ render(controller(
'AppBundle:Attendee:index', { 'request': request, 'id': entity.id }
)) }}
</div>
参加者控制器.php:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class AttendeeController extends Controller
{
public function indexAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$attendees = $em->getRepository('AppBundle:Attendee')->findEventAttendees($id);
$event = $em->getRepository('AppBundle:Event')->findOneById($id);
$paginator = $this->get('knp_paginator');
$pagination2 = $paginator->paginate(
$attendees,
$this->get('request')->query->getInt('page', 1), 10
);
return $this->render('Attendee/index.html.twig', array(
'pagination2' => $pagination2,
'event' => $event,
));
}
}
控制器从 AttendeeRepository 调用函数findEventAttendees,该函数查找与事件关联的参与者:
public function findEventAttendees($id)
{
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder()
->select('a')
->from('AppBundle:Attendee', 'a')
->leftJoin('a.event', 'e')
->where('e.id = :id')
->setParameter('id', $id);
return $qb->getQuery();
}
分页视图由Attendee/index.html.twig呈现:
{% if pagination2.getTotalItemCount > 0 %}
<table class="records_list table">
<thead>
<tr>
<th {% if pagination2.isSorted('a.firstName') %} class="sorted" {% endif %}>
{{ knp_pagination_sortable(pagination2, 'Name', 'a.firstName') }}
</th >
<th {% if pagination2.isSorted('a.uni') %} class="sorted" {% endif %}>
{{ knp_pagination_sortable(pagination2, 'UNI', 'a.uni') }}
</th>
</tr>
</thead>
<tbody>
{% for attendee in pagination2 %}
<tr>
<td>{{ attendee.firstName }} {{ attendee.lastName }}</td>
<td>{{ attendee.uni }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{# display navigation #}
<div class="text-center">
{{ knp_pagination_render(pagination2) }}
</div>
{% else %}
<h2>Sorry, no attendees were found for the specified event.</h2>
{% endif %}
感谢您的任何提示!