2

I am using Knp Paginator Bundle in Symfony 2.

i keep getting the following error when trying to pass a Doctrine query to the paginator:

Fatal error: Call to a member function toArray() on a non-object in /var/www/adwind/vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/ORM/QuerySubscriber.php on line 67 Call Stack: 0.0000 635448 1. {main}() /var/www/adwind/web/app_dev.php:0 0.0166 6042728 2. Symfony\Component\HttpKernel\Kernel->handle() /var/www/adwind/web/app_dev.php:25 0.2128 10057152 3. Symfony\Bundle\FrameworkBundle\HttpKernel->handle() /var/www/adwind/app/bootstrap.php.cache:617 0.2129 10058808 4. Symfony\Component\HttpKernel\HttpKernel->handle() /var/www/adwind/app/bootstrap.php.cache:1566 0.2129 10058808 5. Symfony\Component\HttpKernel\HttpKernel->handleRaw() /var/www/adwind/app/bootstrap.php.cache:1390 2.1839 21851024 6. call_user_func_array() /var/www/adwind/app/bootstrap.php.cache:1426 2.1840 21851640 7. Adwind\BannerBundle\Controller\BannerController->listAction() /var/www/adwind/app/bootstrap.php.cache:1426 2.2085 22257080 8. Knp\Component\Pager\Paginator->paginate() /var/www/adwind/src/Adwind/BannerBundle/Controller/BannerController.php:76 2.2173 22401584 9. Symfony\Component\HttpKernel\Debug\ContainerAwareTraceableEventDispatcher->dispatch() /var/www/adwind/vendor/knplabs/knp-components/src/Knp/Component/Pager/Paginator.php:97 2.2173 22403912 10. Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() /var/www/adwind/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php:78 2.2175 22406864 11. Symfony\Component\EventDispatcher\EventDispatcher->dispatch() /var/www/adwind/app/cache/dev/classes.php:5072 2.2176 22410128 12. Symfony\Component\HttpKernel\Debug\ContainerAwareTraceableEventDispatcher->doDispatch() /var/www/adwind/app/cache/dev/classes.php:4854 2.2254 22490968 13. call_user_func() /var/www/adwind/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php:139 2.2254 22491024 14. Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\QuerySubscriber->items() /var/www/adwind/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php:139

i am using the example from the github readme :

$em    = $this->get('doctrine.orm.entity_manager');
$dql   = "SELECT a FROM AdwindBannerBundle:Banner a";
$query = $em->createQuery($dql);
$pagination = $this->get('knp_paginator')->paginate($query,$page,10);

Everything works fine if i pass the result array instead of the query itself. ie :

$query = $em->createQuery($dql)->getResult();

But as far as i understand it you should be able to pass the query itself or even a query builder. Also i imagine this way i am getting all the results instead of just one page.

ps : i am using dev-master version of the paginator bundle

4

2 に答える 2

3

composer.json で特別なバージョンの knp-сcomponents を使用してみてください

"knplabs/knp-components": "1.2.1",

現在のバージョンにはバグがあると思います

少なくとも今は私にとってはうまくいきます

于 2013-02-22T13:33:20.467 に答える
1

これは私のために働く:

$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQueryBuilder();

$query->select(array('b'))
        ->from('MyMainBundle:Customer', 'b')
        ->orderBy('b.updated_at', 'desc');

$paginator = $this->get('knp_paginator');
$requests = $paginator->paginate($query, 
                                $this->get('request')->query->get('page', 1), 5);

現在、ページネーターはページネーションできます:

  • 配列
  • Doctrine\ORM\Query
  • Doctrine\ORM\QueryBuilder
  • Doctrine\ODM\MongoDB\Query\Query
  • Doctrine\ODM\MongoDB\Query\Builder
  • Doctrine\Common\Collection\ArrayCollection - を含むドクトリン関係コレクション
  • ModelCriteria - ORM クエリを推進する
  • Solarium_Client と Solarium_Query_Select を要素とする配列
于 2013-02-22T08:09:25.433 に答える