Symfony 3 - KNP 分页器错误 - 路由问题?
Symfony 3 - KNP paginator error - Route issue?
我使用 KNP Paginator 添加了分页。除了为特定 ID 呈现页面的页面外,它运行良好。在我添加分页之前,该页面运行良好。函数是
public function orderDetailAction($id)
对于分页,我添加了请求。
/**
* 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()));
}
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 %}
我有以下错误
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\twigf10f12ae3f80d30f6ac9425ed3eadb7f6a850a4574537165108f4cd8dfd500.php
on line 107
controller route是@Route("/order/{id}" paginator用的是这种route ?page=2 不知道是不是这个问题
我该如何解决?
你犯了一些错误:
1) 您可以将查询对象而不是详细信息对象传递给分页方法(您不需要执行查询来对结果进行分页:knp 会为您完成)
2) 您需要将结果传递给视图而不是详细信息对象。
所以修改你的控制器如下:
**
* 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())
);
}
希望对您有所帮助
问题是 route.I 更改了路由和函数参数以从 url 检索 ID 和页面。我进行了以下更改以使其正常工作。
/**
* 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
);
我没有在 $pagination 中使用 get(page,1),而是使用 $page。
的回答对我帮助很大
我使用 KNP Paginator 添加了分页。除了为特定 ID 呈现页面的页面外,它运行良好。在我添加分页之前,该页面运行良好。函数是
public function orderDetailAction($id)
对于分页,我添加了请求。
/**
* 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()));
}
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 %}
我有以下错误
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\twigf10f12ae3f80d30f6ac9425ed3eadb7f6a850a4574537165108f4cd8dfd500.php on line 107
controller route是@Route("/order/{id}" paginator用的是这种route ?page=2 不知道是不是这个问题
我该如何解决?
你犯了一些错误:
1) 您可以将查询对象而不是详细信息对象传递给分页方法(您不需要执行查询来对结果进行分页:knp 会为您完成)
2) 您需要将结果传递给视图而不是详细信息对象。
所以修改你的控制器如下:
**
* 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())
);
}
希望对您有所帮助
问题是 route.I 更改了路由和函数参数以从 url 检索 ID 和页面。我进行了以下更改以使其正常工作。
/**
* 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
);
我没有在 $pagination 中使用 get(page,1),而是使用 $page。