简体   繁体   English

Zend Framework 2 Doctrine 2分页结果

[英]Zend Framework 2 Doctrine 2 paginated results

I've taken the Doctrine 2 and ZF2 installation, with pagination, and tried to apply a search function to the method. 我已经安装了带有分页的Doctrine 2和ZF2,并尝试将搜索功能应用于该方法。

The album list is accessible via the usual route http://myapp/album 相册列表可通过常规途径http://myapp/album

My current code appears to build a usable query using doctrine's createQueryBuilder() 我当前的代码似乎是使用主义的createQueryBuilder()建立可用的查询

When I give no parameters to title/artist then the result of $qb->getDql(); 当我没有给标题/艺术家提供任何参数时,则$qb->getDql();的结果$qb->getDql(); is SELECT a FROM Album\\Entity\\Album a and it returns no error and paginated results fine . SELECT a FROM Album\\Entity\\Album a ,它不返回错误并且分页结果为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(); 但是,当我通过URL http://myapp/album?title=In%20My%20Dreams传递查询时, $qb->getDql();的结果$qb->getDql(); is SELECT a FROM Album\\Entity\\Album a WHERE title like :title which returns the error below. SELECT a FROM Album\\Entity\\Album a WHERE title like :title ,它返回下面的错误。

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;
}

I was getting the same error. 我遇到了同样的错误。 In my case the problem was "date.timezone" not being set in php.ini. 就我而言,问题是未在php.ini中设置“ date.timezone”。 This was causing the following code to actually catch a \\DateTime() exception. 这导致以下代码实际捕获\\ DateTime()异常。

Zend\\Paginator\\Paginator Zend的\\分页程序\\分页程序

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

除了不良的DQL之外,当data / DoctrineORMModule / Proxy没有设置正确的写权限时,也会发生“产生迭代器错误”:

chmod 755 data/DoctrineORMModule/Proxy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM