使用Zend Db Sql进行分页

时间:2016-10-13 05:37:14

标签: php zend-framework pagination zend-framework2

我在zend文档中关注this tutorial,从头开始创建博客模块,完成后一切正常。 在上一个用于创建相册模块的教程中,他们使用TableGateway并展示了如何使用它创建分页,但在此博客模块中,他们没有使用Zend\Db\Sql

我试图在互联网上找到关于那个而没有什么的东西......

public function findAllPosts()
{
    $sql       = new Sql($this->db);
    $select    = $sql->select('posts');
    $statement = $sql->prepareStatementForSqlObject($select);
    $result    = $statement->execute();

    if (! $result instanceof ResultInterface || ! $result->isQueryResult()) {
        return [];
    }

    $resultSet = new HydratingResultSet($this->hydrator, $this->postPrototype);
    $resultSet->initialize($result);
    return $resultSet;
}

在上一个教程中,我们创建了两个方法,一个名为fetchAll($paginated = false),另一个名为fetchPaginatedResults,如果$paginated为真,则调用方法fetchPaginatedResults,例如:

public function fetchAll($paginated = false)
{
    if ($paginated) {
        return $this->fetchPaginatedResults();
    }

    return $this->tableGateway->select();
}

private function fetchPaginatedResults()
{
    // Create a new Select object for the table:
    $select = new Select($this->tableGateway->getTable());

    // Create a new result set based on the Album entity:
    $resultSetPrototype = new ResultSet();
    $resultSetPrototype->setArrayObjectPrototype(new Album());

    // Create a new pagination adapter object:
    $paginatorAdapter = new DbSelect(
        // our configured select object:
        $select,
        // the adapter to run it against:
        $this->tableGateway->getAdapter(),
        // the result set to hydrate:
        $resultSetPrototype
    );

    $paginator = new Paginator($paginatorAdapter);
    return $paginator;
}

我想知道,如何使用Zend\Db\SqlHydratingResultSet来做同样的事情?

2 个答案:

答案 0 :(得分:0)

您必须选择$db_adapter并获得分页符:

use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\DbSelect;

$sql = new Sql($this->db);
$select = $sql->select('posts');
new Paginator(new DbSelect($select, $db_adpter));

答案 1 :(得分:0)

你可以在Paginator中设置HydratingResultSet。

示例:

    $sql = new Sql ( $this->db );
    $select = $sql->select ( $this->table );

    //$statement = $sql->prepareStatementForSqlObject ( $select );
    //$result = $statement->execute ();     
    //if (! $result instanceof ResultInterface || ! $result->isQueryResult ()) {return [ ];}

    $resultSet = new HydratingResultSet ( $this->hydrator, $this->Prototype );
    //$resultSet->initialize ( $result );
    // return $resultSet;

    // Executando
    $adapter = new \Zend\Paginator\Adapter\DbSelect ( $select, $this->db, $resultSet );
    $paginator = new \Zend\Paginator\Paginator ( $adapter );
    $paginator->setItemCountPerPage ( $count );
    $paginator->setCurrentPageNumber ( $offset );

    return $paginator;

但如果您使用Paginator,我不确定Hydrator是否是最佳选择。

记住,你需要安装包(如果它是zf3)

$ composer require zendframework/zend-paginator
Using version ^2.7 for zendframework/zend-paginator
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing zendframework/zend-paginator (2.7.0)
    Loading from cache


  Please select which config file you wish to inject 'Zend\Paginator' into:
  [0] Do not inject
  [1] config/modules.config.php
  [2] config/development.config.php.dist
  Make your selection (default is 0):1

  Remember this option for other packages of the same type? (y/N)
Installing Zend\Paginator from package zendframework/zend-paginator
zendframework/zend-paginator suggests installing zendframework/zend-cache (Zend\Cache component to support cache features)
Writing lock file
Generating autoload files