简体   繁体   English

使用Zend Db Sql进行分页

[英]Pagination using Zend Db Sql

I'm following this tutorial on zend documentation to create the blog module from the beginning, finished and everything is working properly. 我正在关注zend文档的本教程 ,从头开始创建博客模块,完成所有工作正常。 On the previous tutorial that was to create an album module they used TableGateway and also showed how to create the pagination with it, but in this blog module they didn't and they are using Zend\\Db\\Sql . 在上一个用于创建相册模块的教程中,他们使用了TableGateway并且还展示了如何使用它创建分页,但是在这个博客模块中他们没有,并且他们使用的是Zend\\Db\\Sql

I tried to find something on the internet about that and nothing... 我试图在互联网上找到关于这一点的东西......

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

On the previous tutorial we created two methods, one called fetchAll($paginated = false) and another one called fetchPaginatedResults , if $paginated was true it called the method fetchPaginatedResults , for example: 在上一个教程中,我们创建了两个方法,一个名为fetchAll($paginated = false) ,另一个名为fetchPaginatedResults ,如果$paginated为true,则调用方法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;
}

I was wondering, how to do the same using Zend\\Db\\Sql and HydratingResultSet ? 我想知道,如何使用Zend\\Db\\SqlHydratingResultSet做同样的事情?

You must pick $db_adapter and you will get the paginator by : 你必须选择$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));

you can set HydratingResultSet in Paginator. 你可以在Paginator中设置HydratingResultSet。

Example: 例:

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

but if you will use Paginator, I'm not sure if the Hydrator is the best option. 但如果您使用Paginator,我不确定Hydrator是否是最佳选择。

Remember , you need to install the package (if it is zf3) 记住 ,你需要安装包(如果它是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM