Symfony2 KnpPaginator bundle, cannot do sorting with 2 paginated tables in my view

StackOverflow https://stackoverflow.com/questions/10480661

  •  06-06-2021
  •  | 
  •  

문제

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!

다른 팁

It's 2017 and I got into similar problem. Just for reference, I'm using:

    "knplabs/knp-paginator-bundle": "^2.5",

I was trying to run several functional tests with Symfony's crawler. Some of them were using Knp Paginator with such options:

[
    'defaultSortFieldName' => 'voucher.id',
    'defaultSortDirection' => 'DESC',
    'wrap-queries' => true,
    'defaultFilterFields' => ['voucherText.title']
]

while others with those options:

[
    'defaultSortFieldName' => 'event.id',
    'defaultSortDirection' => 'DESC',
    'wrap-queries' => true,
    'defaultFilterFields' => ['event.name', 'eventText.description']
]

Problem appeared with sorting parameter name, because both Paginators were using defaul 'sort' key, taking it from global $_GET array - you can check it yourself: Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ORM\QuerySubscriber.

However the enigmatic part was the error being thrown:

UnexpectedValueException: There is no component aliased by [event] in the given Query

In order to avoid this weirdos/crappiness, I found a 'sortFieldParameterName' option, which you can add to all of your Paginators' options:

[
    'sortFieldParameterName' => 'sort_event',
    'defaultSortFieldName' => 'event.id',
    'defaultSortDirection' => 'DESC',
    'wrap-queries' => true,
]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top