Zend Framework 2分页器+ TableGateway [英] Zend Framework 2 Paginator + TableGateway

查看:149
本文介绍了Zend Framework 2分页器+ TableGateway的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解如何使用下面的代码实现DbSelect分页器时遇到了一些麻烦(现在它使用的是Iterator适配器,不适用于ResultSets).

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).

据我所知,这并不像我希望的那样直接. DbSelect需要一个Zend\Db\Sql\Select和一个适配器.适配器不是问题,可以通过以下方式检索:

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()

但是我在不复制查询代码的情况下无法从TableGateway中获取Select对象的问题.有没有简单的方法来解决这个问题?

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

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

}

推荐答案

使用DbSelect作为适配器实现分页器

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

这篇关于Zend Framework 2分页器+ TableGateway的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆