我采用了分页的Doctrine 2和ZF2安装,并尝试将搜索功能应用于该方法。
可以通过常规路线http://myapp/album
我当前的代码似乎是使用doctrine的createQueryBuilder()
如果我没有为标题/艺术家提供参数,则$qb->getDql();
的结果为SELECT a FROM Album\Entity\Album a
,并返回无错误且分页结果正常
我的方法;
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;
}
但是,当我通过网址http://myapp/album?title=In%20My%20Dreams
传递查询时,$qb->getDql();
的结果为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}
我理解错误是视图访问变量的位置,但是当paginator处理修改后的查询时,它似乎在错误之前得到了视图。我不太确定这里发生了什么。
为避免疑义,原始方法也可以正常运行;
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;
}
答案 0 :(得分:1)
我得到了同样的错误。就我而言,问题是" date.timezone"没有在php.ini中设置。这导致以下代码实际捕获\ DateTime()异常。
的Zend \分页程序\分页程序
public function getIterator()
{
try {
return $this->getCurrentItems();
} catch (\Exception $e) {
throw new Exception\RuntimeException('Error producing an iterator', null, $e);
}
}
答案 1 :(得分:0)
除了糟糕的DQL,"错误产生迭代器"当data / DoctrineORMModule / Proxy没有设置正确的写权限时,也会发生这种情况:
chmod 755 data/DoctrineORMModule/Proxy