2014-02-20 92 views
0

我寫的代碼在下面給出控制器,無法獲取ZF2分頁

$iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($this->getCategoryTable()->listCategoryData()); 
$paginatorDatas = new \Zend\Paginator\Paginator($iteratorAdapter); 
$paginatorDatas->setCurrentPageNumber($page)->setItemCountPerPage(5); 

,我使變量$ paginatorDatas查看文件。

在對應的視圖文件中,我已經寫這樣的代碼,

echo $this->paginationControl($this->paginatorDatas, 'Elastic', 'paginator.phtml'); 

之前調用$這 - > paginationControl()函數我已經迭代數組對象($ paginatorDatas),通過foreach(){}填充記錄。

但上面的代碼提供了以下錯誤,

致命錯誤 :未捕獲的異常「的Zend \ DB \適配器\異常\的RuntimeException」與消息「這一結果是僅正向結果集,調用倒帶()之後向前移動不支持'/var/www/cp/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Result.php:153堆棧軌跡:#0 [內部函數]:Zend \ Db \ Adapter \ Driver \ Pdo \ Result-> rewind()#1 /var/www/cp/module/Admin/view/admin/category/ajax-list.phtml(21):LimitIterator-> rewind()#2/var/www/cp/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php(507):include('/ var/www/cp/mod ...')#3/var/www/cp/vendor/zendframework/zendframework/library/Zend/View/View.php(205):Zend \ View \ Renderer \ PhpRenderer-> ren der(Object(Zend \ View \ Model \ ViewModel))#4 /var/www/cp/vendor/zendframework/zendframework/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php(102):Zend \ View \ View - > render(Object(Zend \ View \ Model \ ViewModel))#5 [內部函數]:Zend \ Mvc \ View \ Http \ DefaultRenderingStrategy-> render(Object(Zend \ Mvc \ MvcEvent))#6/var/www在線/ CP /在/var/www/cp/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Result.php 153

請幫我....

回答

1

方法1

$resultSet = $this->getCategoryTable()->listCategoryData(); 
$resultSet->buffer(); 
$resultSet->next(); 
$iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($resultSet); 
$paginatorDatas = new \Zend\Paginator\Paginator($iteratorAdapter); 
$paginatorDatas->setCurrentPageNumber($page)->setItemCountPerPage(5); 

從從Samsonasik

this教程

方法2

$resultSet = $this->getCategoryTable()->listCategoryData(); 
$resultSet = iterator_to_array($resultSet); 
$adapter = new \Zend\Paginator\Adapter\ArrayAdapter($resultSet); 
$paginatorDatas = new \Zend\Paginator\Paginator($adapter); 
$paginatorDatas->setCurrentPageNumber($page)->setItemCountPerPage(5); 
+0

方法2適用於我...謝謝... 第一種方法拋出相同的錯誤... – EKL

+0

@EKL然後,考慮接受並upvoting這個答案! –

0

用您的查詢結果填充陣列,您可以使用\Zend\Paginator\Adapter\ArrayAdapter代替\Zend\Paginator\Adapter\Iterator

+0

這個方法我試過,但還是我收到以下錯誤...... 捕獲的致命錯誤:參數1傳遞給Zend \ Paginator \ Adapter \ ArrayAdapter :: __ construct()必須是一個數組,對象給出,在/var/www/cp/module/Admin/src/Admin/Controller/CategoryController.php中調用74a行nd定義在/var/www/cp/vendor/zendframework/zendframework/library/Zend/Paginator/Adapter/ArrayAdapter.php 33行 – EKL

+0

這就是爲什麼我說你的查詢結果爲POPULATE數組,而不是直接使用它。 ;) – jmleroux