Zend框架2:分页仅显示来自DB的一半项目

时间:2015-09-15 13:53:08

标签: php database pagination zend-framework2

我从数据库加载一些数据,有554行。但并非所有都加载到网站上。当我将paginator->setItemCountPerPage()设置为10时,会显示280个项目。当我将paginator->setItemCountPerPage()设置为1时,恰好显示了277个项目(所有项目的数量的一半)。那是为什么?

控制器

<?php
namespace Work\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Paginator\Paginator;
use Zend\View\Model\ViewModel;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\Iterator as paginatorIterator;

class WorkController extends AbstractActionController {

    protected $workTable;

    public function indexAction() {

        $select = new Select();
        $order_by = 'datetime';
        $order = Select::ORDER_ASCENDING;
        $works = $this->getWorkTable()->fetchAll($select->order($order_by . ' ' . $order));
        $works->current();
        $page = 1;

        if ($this->params()->fromRoute('page')) {
            $page = $this->params()->fromRoute('page');
        }
        $paginator = new Paginator(new paginatorIterator($works));
        $paginator->setCurrentPageNumber($page)
                ->setItemCountPerPage(10)
                ->setPageRange(10);

        return new ViewModel(array('paginator' => $paginator));
    }

    public function getWorkTable() {
        if (!$this->workTable) {
            $sm = $this->getServiceLocator();
            $this->workTable = $sm->get('Work\Model\WorkTable');
        }
        return $this->workTable;
    }
}

工作台

<?php
namespace Work\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;

class WorkTable {
    protected $table = 'works';
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway) {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll(Select $select = null) {
        if (null === $select) {
            $select = new Select();
        }
        $select->from($this->table);
        $resultSet = $this->tableGateway->selectWith($select);
        $resultSet->buffer();
        return $resultSet;
    }
}

工作

<?php
namespace Work\Model;

use Zend\Db\Adapter\Adapter;

class Work
{
    public $name;
    public $dateTime;

    protected $dbAdapter;

    public function __construct(Adapter $dbAdapter) {
        $this->dbAdapter = $dbAdapter;
    }

    public function exchangeArray($data) {
        //... more columns
        $this->name  = (isset($data['name'])) ? $data['name'] : null;
        $this->dateTime = (isset($data['datetime'])) ? $data['datetime'] : null;
    }

    public function getArrayCopy() {
        return get_object_vars($this);
    }    
}

index.phtml

<?php
$title = 'Something';
$this->headTitle($title);
?>
<header>
    <h1><?php echo $this->escapeHtml($title); ?></h1>
</header>

<section>
    <script>
        $(document).ready(function () {
            $("a.gallery-image").fancybox({openEffect: 'none', closeEffect: 'none'});
        });
    </script>
    <?php echo $this->paginationControl( $this->paginator, 'Sliding', 'pagination.phtml', array('route' => 'work'));?>
    <?php foreach ($this->paginator as $work) : ?>
        <div class="gallery">
            <?php list($width, $height, $type, $attr) = getimagesize(getcwd() . '/public' . "/obrazky/rucne-prace/velke/" . $work->imgSrc); ?>
            <div class="gallery-text"><?php echo $this->escapeHtml($work->name); ?></div>
        </div>
    <?php endforeach; ?>
    <div class="clear"></div>
    <?php echo $this->paginationControl( $this->paginator, 'Sliding', 'pagination.phtml', array('route' => 'work'));?>
</section>

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题。它看起来Zend中有一个错误。使用PDO驱动程序无法读取结果集两次。所以我将PDO驱动程序更改为Mysqli。

<?php
return array(
    'db' => array(
        'driver' => 'Mysqli', 
        'host' => 'localhost', 
        'database' => 'database_name', 
        'charset'  => 'utf8',
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter'
                    => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);