我在Symfony 2中使用Knp分页器捆绑包。
当我试图将条令查询传递给分页器时,我一直收到以下错误:
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. SymfonyComponentHttpKernelKernel->handle() /var/www/adwind/web/app_dev.php:25 0.2128 10057152 3. SymfonyBundleFrameworkBundleHttpKernel->handle() /var/www/adwind/app/bootstrap.php.cache:617 0.2129 10058808 4. SymfonyComponentHttpKernelHttpKernel->handle() /var/www/adwind/app/bootstrap.php.cache:1566 0.2129 10058808 5. SymfonyComponentHttpKernelHttpKernel->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. AdwindBannerBundleControllerBannerController->listAction() /var/www/adwind/app/bootstrap.php.cache:1426 2.2085 22257080 8. KnpComponentPagerPaginator->paginate() /var/www/adwind/src/Adwind/BannerBundle/Controller/BannerController.php:76 2.2173 22401584 9. SymfonyComponentHttpKernelDebugContainerAwareTraceableEventDispatcher->dispatch() /var/www/adwind/vendor/knplabs/knp-components/src/Knp/Component/Pager/Paginator.php:97 2.2173 22403912 10. SymfonyComponentEventDispatcherContainerAwareEventDispatcher->dispatch() /var/www/adwind/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php:78 2.2175 22406864 11. SymfonyComponentEventDispatcherEventDispatcher->dispatch() /var/www/adwind/app/cache/dev/classes.php:5072 2.2176 22410128 12. SymfonyComponentHttpKernelDebugContainerAwareTraceableEventDispatcher->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. KnpComponentPagerEventSubscriberPaginateDoctrineORMQuerySubscriber->items() /var/www/adwind/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php:139
我使用的是github自述文件中的示例:
$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);
如果我传递的是结果数组而不是查询本身,那么一切都会很好。即:
$query = $em->createQuery($dql)->getResult();
但据我所知,您应该能够传递查询本身,甚至传递查询生成器。此外,我想象通过这种方式,我可以得到所有的结果,而不是一页。
ps:我使用的是分页器捆绑包的dev-master版本
尝试在composer.json 中使用特殊版本的knp-сcomponents
"knplabs/knp组件":"1.2.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);
当前分页器可以分页:
- 阵列
- 条令\ORM\查询
- 条令\ORM\QueryBuilder
- Doctrine\ODM\MongoDB \Query\Query
- Doctrine\ODM\MongDB\Query\Builder
- 条令\普通\收藏\阵列收藏-任何条令关系集合,包括
- ModelCriteria-Propel ORM查询
- 以Solarium_Client和Solarium_Query_Select为元素的数组