简体   繁体   English

Symfony 3-KNP分页器错误-路由问题?

[英]Symfony 3 - KNP paginator error - Route issue?

I added a pagination using KNP Paginator. 我使用KNP分页器添加了分页。 It works well except for one page that render a page for specific ID. 除了一页显示特定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 Twig文件是

{% 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 类型错误:传递给Knp \\ Bundle \\ PaginatorBundle \\ Twig \\ Extension \\ PaginationExtension :: render()的参数2必须是Knp \\ Bundle \\ PaginatorBundle \\ Pagination \\ SlidingPagination的实例,给定数组,在C:\\ xampp \\ htdocs \\中调用在第107行上的库存\\ var \\ cache \\ dev \\ twig \\ 60 \\ 60f10f12ae3f80d30f6ac9425ed3eadb7f6a850a4574537165108f4cd8dfd500.php

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. 控制器路由为@Route(“ / order / {id}”,分页器使用这种路由?page = 2。我不知道这是否是问题。

How can I solve it? 我该如何解决?

You make some mistake: 您犯了一些错误:

1) You can pass the query object to the paginate method instead of the details object (You don't need to execute the query in order to paginate the result : knp do it for you) 1)您可以将查询对象传递给paginate方法而不是details对象(您无需执行查询即可对结果进行分页:knp为您完成)

2) You need to pass the result to the view instead of the details object. 2)您需要将结果传递给视图,而不是详细信息对象。

So modify your controller as follow: 因此,如下修改您的控制器:

**
  * 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();

    $paginator = $this->get('knp_paginator');

    $result = $paginator->paginate(
      $query,  // (1) pass the query instead of the result (better performance)
      $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'=> $result,  // (2) pass the result of the pagination 
    'form' => $form->createView())
    );

  }

Hope this help 希望有帮助

The problem was the route.I changed the route and the function parameters to retrieve the id and page from the url. 问题是路线,我更改了路线和功能参数以从url中检索ID和页面。 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. 我没有在$ pagination中使用get(page,1),而是使用$ page。

The answer of Paginate in KnpPager not work help me a lot. KnpPager分页的答案不起作用对我有很大帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM