Knp 分页器不分页

Knp paginator not paginating

我在让 Knp 分页器前进到下一页时遇到问题。页面导航栏显示正确,如 this image 中所示(名称是假的),并且排序有效。但是,当我尝试前进到第 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>

AttendeeController.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 %}

感谢您的任何提示!

在控制器中,使用作为参数传递的 Request 对象而不是从容器中检索的 Request。

所以试试这个:

$request->query->getInt('page', 1); 

而不是这个:

$this->get('request')->query->getInt('page', 1);

从容器中获取请求是一个废弃的特性。在 this annuncement 中,Fabien 看到:

Dealing with the request in the service container is difficult to say the least. Why is it difficult to inject the request into a service? Because you can have more than one request in a single PHP process (think sub-requests.) So, during the lifetime of the container, the request instance changes.

希望对您有所帮助