Zend Framework 2 Paginator + TableGateway

时间:2012-09-15 20:52:23

标签: zend-framework2 zend-db zend-paginator tablegateway

如何将数据库映射器与paginator一起使用?

我在理解如何使用下面的代码实现DbSelect paginator时遇到了一些麻烦(现在它正在使用Iterator适配器,它不适用于ResultSet)。

据我所知,这并不像我希望的那样直截了当。 DbSelect期待Zend\Db\Sql\Select和适配器。适配器不是问题,可以使用以下方法检索:

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

但是我在从TableGateway获取Select对象时遇到问题而没有复制我的查询代码。有没有简单的方法来解决这个问题?

NewsController.php

<?php

namespace News\Controller;

use Zend\Paginator\Paginator;

class NewsController extends \Application\Controller\WebsiteController
{
    protected $newsTable;

    protected $newsContents;

    protected function newsTable()
    {
        return $this->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

<?php

namespace News\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Select;

class NewsContentsTable extends \Zend\Db\TableGateway\AbstractTableGateway
{
    protected $table = 'news_contents';

    public function __construct(Adapter $adapter)
    {
        $this->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;
    }

}

5 个答案:

答案 0 :(得分:7)

从Zend Framework 2.2开始,它更容易(并且让您充分享受TableGateway提供的优势) - 您应该使用

use Zend\Db\TableGateway\TableGateway;
use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\DbTableGateway;  // !!!

 
$dbTableGatewayAdapter = new DbTableGateway($this->tableGateway);
$paginator = new Paginator($dbTableGatewayAdapter);
return $paginator;

答案 1 :(得分:4)

使用Db选择实现分页选择作为适配器

// 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 :(得分:0)

在$ rowset中返回之前使用缓冲区()和 next()

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

return $rowset;

答案 3 :(得分:0)

  
    

但是我在从TableGateway中获取Select对象时遇到问题而没有复制我的查询代码。有没有简单的方法来解决这个问题?

  

你可以在你的NewsContentsTable类中获取select对象:

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

答案 4 :(得分:0)

以下是ZF2中分页的示例代码

 <?php
    //var/www/html/zend_app/module/Application/src/Application/Controller/Plugin/MyCustomPlugin.php
    namespace Application\Controller\Plugin;

    use Zend\View\Helper\AbstractHelper;

    use Zend\Db\Adapter\Adapter;
    use Zend\Db\ResultSet\ResultSet;
    use Zend\Db\Sql\Select;
    use Zend\Db\Sql\Sql;
    use Zend\Db\Sql\Where;

    use Zend\Mvc\Controller\Plugin\AbstractPlugin;

    use Zend\Paginator\Paginator;
    use Zend\Paginator\Adapter\Iterator as paginatorIterator;
    use Zend\Paginator\Adapter\DbSelect;

    class MyCustomPlugin extends AbstractPlugin {

        protected $adapter;

        public function __construct(\Zend\Db\Adapter\Adapter $adapter) {
            $this->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