dongre6227 2014-03-12 15:46
浏览 67

Zend Framework 2 Doctrine 2分页结果

I've taken the Doctrine 2 and ZF2 installation, with pagination, and tried to apply a search function to the method.

The album list is accessible via the usual route http://myapp/album

My current code appears to build a usable query using doctrine's createQueryBuilder()

When I give no parameters to title/artist then the result of $qb->getDql(); is SELECT a FROM Album\Entity\Album a and it returns no error and paginated results fine.

My method;

public function indexAction()
{
  $view =  new ViewModel();

  $repository = $this->getEntityManager()->getRepository('Album\Entity\Album');
  $qb = $repository->createQueryBuilder('album');
  $qb->add('select', 'a')
     ->add('from', 'Album\Entity\Album a');
  if ($this->params()->fromQuery('title')) {
    $qb->add('where','title like :title');
    $qb->setParameter(':title', '%' . $this->params()->fromQuery('title') . '%');
  }
  if ($this->params()->fromQuery('artist')) {
    $qb->add('where','artist like :artist');
    $qb->setParameter(':artist', '%' . $this->params()->fromQuery('artist') . '%');
  }
  $q = $qb->getDql();
  $paginator = new Paginator(new DoctrineAdapter(new ORMPaginator($this->getEntityManager()->createQuery($q))));
  $paginator->setDefaultItemCountPerPage(10);

  $page = (int)$this->params()->fromQuery('page');
  if ($page) $paginator->setCurrentPageNumber($page);
  $view->setVariable('paginator', $paginator);

  return $view;
}

But when I pass a query through the URL http://myapp/album?title=In%20My%20Dreams the result of $qb->getDql(); is SELECT a FROM Album\Entity\Album a WHERE title like :title which returns the error below.

Zend\Paginator\Exception\RuntimeException
File:
/Users/luke/Sites/hotelio/vendor/zendframework/zendframework/library/Zend/Paginator/Paginator.php:637
Message:
Error producing an iterator
Stack trace:
#0 /myapp/module/Album/view/album/album/index.phtml(20): Zend\Paginator\Paginator->getIterator()
#1 /myapp/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php(507): include('/myapp...')
#2 /myapp/vendor/zendframework/zendframework/library/Zend/View/View.php(205): Zend\View\Renderer\PhpRenderer->render(Object(Zend\View\Model\ViewModel))
#3 /myapp/vendor/zendframework/zendframework/library/Zend/View/View.php(233): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#4 /myapp/vendor/zendframework/zendframework/library/Zend/View/View.php(198): Zend\View\View->renderChildren(Object(Zend\View\Model\ViewModel))
#5 /myapp/vendor/zendframework/zendframework/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php(102): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#6 [internal function]: Zend\Mvc\View\Http\DefaultRenderingStrategy->render(Object(Zend\Mvc\MvcEvent))
#7 /myapp/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#8 /myapp/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('render', Object(Zend\Mvc\MvcEvent), Array)
#9 /myapp/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(347): Zend\EventManager\EventManager->trigger('render', Object(Zend\Mvc\MvcEvent))
#10 /myapp/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(322): Zend\Mvc\Application->completeRequest(Object(Zend\Mvc\MvcEvent))
#11 /myapp/public/index.php(12): Zend\Mvc\Application->run()
#12 {main}

I understand that the error is where the view is accessing the variable, but as the paginator handles the modified query it appears to get as far of the view before erroring. I am not really sure what is going on here.

For the avoidance of doubt, the original method which also operates without error;

public function indexAction()
{
  $view =  new ViewModel();

  $repository = $this->getEntityManager()->getRepository('Album\Entity\Album');

  $paginator = new Paginator(new DoctrineAdapter(new ORMPaginator($this->getEntityManager()->createQuery($repository->createQueryBuilder('album')))));
  $paginator->setDefaultItemCountPerPage(10);

  $page = (int)$this->params()->fromQuery('page');
  if ($page) $paginator->setCurrentPageNumber($page);
  $view->setVariable('paginator', $paginator);

  return $view;
}
  • 写回答

2条回答 默认 最新

  • drd2551 2015-02-05 10:15
    关注

    I was getting the same error. In my case the problem was "date.timezone" not being set in php.ini. This was causing the following code to actually catch a \DateTime() exception.

    Zend\Paginator\Paginator

        public function getIterator()
        {
            try {
                return $this->getCurrentItems();
            } catch (\Exception $e) {
                throw new Exception\RuntimeException('Error producing an iterator', null, $e);
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败