I have 1 controller that passes 2 paginated set of results to a twig (as 2 arrays representing tables) using KnpPaginator Bundle.
While both tables show up and are paginated, I am not able to sort any of them. Edit: When I change page of table 1, table 2's page also changes to that page number.
Trying to sort either of them returns: There is no component aliased by [t] in the given Query or There is no component aliased by [r] in the given Query.
The controller:
$em = $this->getDoctrine()->getEntityManager();
$pageSize = $this->container->getParameter('page_size');
$paginator = $this->get('knp_paginator');
/* Queries */
$queryTest = $em
->createQuery('
SELECT t FROM PanasonicTestEtAvisBundle:Test t
JOIN t.product p
WHERE p.id = :id
AND t.isDeleted = :isdeleted
ORDER BY t.creationDate DESC'
)->setParameter('id', $id)
->setParameter('isdeleted', '0');
$queryReview = $em
->createQuery('
SELECT r FROM PanasonicTestEtAvisBundle:Review r
JOIN r.product p
WHERE p.id = :id
AND r.isDeleted = :isdeleted
ORDER BY r.creationDate DESC'
)->setParameter('id', $id)
->setParameter('isdeleted', '0');
/* paginated results */
$paginationTest = $paginator->paginate($queryTest, $this->get('request')->query->get('page', 1), $pageSize);
// compact('paginationTest');
$paginationReview = $paginator->paginate($queryReview, $this->get('request')->query->get('page', 1), $pageSize);
// compact('paginationReview');
// compact('pagination');
return $this->render('PanasonicTestEtAvisBundle:Product:show.html.twig', array(
'paginationTest' => $paginationTest,
'paginationReview' => $paginationReview
));
The error shows up only when I pass both pagination (paginationTest & paginationReview), if I pass only one it works flawlessly.
Anyone can tell me how I can solve this problem?
Your example didn't work, because you use the same page variable "page" for both of pagination sets.
If you want to use two or more pagination sets in the same place, you must do this:
Your Controller:
...
$pagename1 = 'page1'; // Set custom page variable name
$page1 = this->get('request')->query->get($pagename1, 1); // Get custom page variable
$paginationReview = $paginator->paginate(
$queryReview,
$page1,
$pageSize,
array('pageParameterName' => $pagename1)
);
$pagename2 = 'page2'; // Set another custom page variable name
$page2 = this->get('request')->query->get($pagename2, 1); // Get another custom page variable
$paginationReview = $paginator->paginate(
$queryReview,
$page2,
$pageSize,
array('pageParameterName' => $pagename2)
);
return $this->render('PanasonicTestEtAvisBundle:Product:show.html.twig', array(
'paginationTest' => $paginationTest,
'paginationReview' => $paginationReview
));
Your view:
// PanasonicTestEtAvisBundle:Product:show.html.twig
{# First set #}
{% for test in paginationTest %}
<tr>
<td>{{ test.id }}</td>
</tr>
{% endfor %}
{{ paginationTest.render()|raw }}
{# Second set #}
{% for review in paginationReview %}
<tr>
<td>{{ review.id }}</td>
</tr>
{% endfor %}
{{ paginationReview.render()|raw }}
Enjoy!