Zend Framework 2 Paginator + TableGateway

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

How to use the database mapper with the paginator?

I'm having a bit trouble understanding how I implement the DbSelect paginator using the code below (right now it's using the Iterator adapter which doesn't work for ResultSets).

From what I can tell it's not as straight forward as I would have hoped. DbSelect is expecting a Zend\Db\Sql\Select and an adapter. The adapter is a non issue and can be retrieved with:

$this->newsContents()->getAdapter() 

but I'm having trouble getting a Select object out from my TableGateway without duplicating my query code. Is there an easy way to solve this problem?

NewsController.php

getServiceLocator()->get('News\Model\NewsTable');     }      protected function newsContents()     {         return $this->getServiceLocator()->get('News\Model\NewsContentsTable');     }      protected function articleId()     {         return (int) $this->params()->fromRoute('id');     }      public function articleAction()     {         $article = $this->newsTable()->getArticle($this->articleId());         $pages   = $this->newsContents()->getPages($this->articleId());          $paginator = new Paginator(new \Zend\Paginator\Adapter\Iterator($pages));         $paginator->setCurrentPageNumber($this->params()->fromRoute('page'));          return array(             'css'       => 'news.css',             'article'   => $article,             'paginator' => $paginator,         );     } } 

NewsContentsTable.php

adapter = $adapter;         $this->resultSetPrototype = new ResultSet;         $this->resultSetPrototype->setArrayObjectPrototype(new NewsContents);         $this->initialize();     }      public function getPages($newsId)     {         $rowset = $this->select(function(Select $select) use ($newsId)         {             $select                 ->order('order ASC')                 ->where(array('news_id' => $newsId));         });          return $rowset;     }  } 

回答1:

Implementing paginator with DbSelect as the adapter

// controller public function articleAction() {     //...     $paginator = $this->newsContents()->getPages($this->articleId());     $paginator->setCurrentPageNumber($this->params()->fromRoute('page'));      return array(         'css'       => 'news.css',         'article'   => $article,         'paginator' => $paginator,     ); } ?>  // table public function getPages($newsId) {     $sql = $this->getSql();     $select = $sql->select();     $select->where(array('news_id' => $newsId))->order('id ASC');     $adapter = new \Zend\Paginator\Adapter\DbSelect($select, $sql);     $paginator = new \Zend\Paginator\Paginator($adapter);     return $paginator; } 


回答2:

From Zend Framework 2.2 its much easier (and allows you to fully enjoy the advantages offered by the TableGateway) - you should use



回答3:

but I'm having trouble getting a Select object out from my TableGateway without duplicating my query code. Is there an easy way to solve this problem?

You can get the select object inside you NewsContentsTable class like this:

$this->getSql()->select(); 


回答4:

Use buffer() and next() before return in $rowset.

$rowset->buffer(); $rowset->next();  return $rowset; 


回答5:

Here is sample code for pagination in ZF2

 adapter = $adapter;         }              public function getPaginatedTableData($tableName, $whereData = "", $selectedColumn = '' , $currentPageNumber = 1) {              $sql = new Sql($this->adapter);             $select = $sql->select();              if ($selectedColumn) {                 $select->columns($selectedColumn);             }             $select->from($tableName);             if ($whereData) {                 $select->where($whereData);             }              $paginator = $this->getPaginatorForSelect($select, $currentPageNumber);               $resultSet = new ResultSet;             $resultSet->initialize($paginator);             $resultSet->buffer();                $pagination = array(                'current_page_number'=>$currentPageNumber,                'page_count'=>$paginator->getPages()->pageCount,                'previous'=>isset($paginator->getPages()->previous)?$paginator->getPages()->previous:0,                'next'=>isset($paginator->getPages()->next)?$paginator->getPages()->next:0,                );    return ($resultSet &&  0 != $paginator->getPages()->pageCount)?array('resultSet'=>$resultSet->toArray(), 'pagination'=>$pagination):FALSE;          }             public function getPaginatorForSelect($select, $page, $limit=2)     {     $paginatorAdapter = new DbSelect($select, $this->adapter);     $paginator = new Paginator($paginatorAdapter);     $paginator->setItemCountPerPage($limit);     $paginator->setPageRange(5);     $paginator->setCurrentPageNumber($page);     return $paginator;     }      } 

HTH



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!