0

我在我的索引操作中使用了 Zend_Paginator,这显示了我所有的目录:

public function indexAction()
{
   Zend_View_Helper_PaginationControl::setDefaultViewPartial('/pagination.phtml');
    $pages = new Application_Model_DbTable_Catalog();

    $num = 10;
    $page = $this->_getParam('page');

    $select = $pages->select();
    $result = $this->view->table = $pages->fetchAll($select)->toArray();
    $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($result));
    $paginator->setItemCountPerPage($num);
    $paginator->setCurrentPageNumber($page);
    $paginator->setView($this->view);
    $this->view->paginator = $paginator;
}

它工作得很好,现在我有了 CategoryAction,它按类别对​​我的目录进行排序

public function categoryAction()
{
    $id = intval($this->_getParam('id', 0));
    if ($id > 0) {
        $catalog = new Application_Model_DbTable_Catalog();
        $this->view->catalog = $catalog->getByCategoryId($id);
        и
        $category = new Application_Model_DbTable_Categories();
        $this->view->category = $category->getById($id);



    }

所以我不明白如何在这个动作中添加分页,帮帮我,Pleaaasssseeeeeeee :-(,抱歉我的英语不好

4

1 回答 1

0

好的,有更好/更简单的方法来做到这一点。

分页从您的数据库查询开始。您正在使用 DbTable 模型,因此在 ZF1 中设置适配器非常容易。

<?php 

class Application_Model_DbTable_Catalog extends Zend_Db_Table_Abstract
{
    protected $_name = 'catalog'
    /*
     * This method returns a paginator adapter with a fetchAll($where = null, $order = null, $count = null, $offset = null)
     * paginator adapter will supply the values for $offset and $limit
     */
    public function getPagedCatalog() 
    {
        $select = $this->select(); //put anything you need into $select
        $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);//the paginator adapter in place of the fetchAll().

        return $adapter;
    {
}

<?php 

class Application_Model_DbTable_Categories extends Zend_Db_Table_Abstract
{
    protected $_name = 'categories'

    /*
     * This method returns a paginator adapter with a fetchAll($where = $id, $order = null, $count = null, $offset = null)
     * paginator adapter will supply the values for $offset and $limit
     */
    public function getPagedCatagories($id) 
    {
        $select = $this->select(); //put anything you need into $select
        $select->where('catagory_id = ?', $id);//you may need to build a join for this to work how you want it to, depends on your table structure.
        $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);//the paginator adapter in place of the fetchAll().

        return $adapter;
    {
}

现在您的模型将返回包含这些表内容的分页器适配器。Zend_Paginator将自动为查询提供$limitand$offset参数,以便每个页面执行一次查询。使用此适配器,整个表将不会存储在内存中。

现在你indexAction()可能看起来像:

public function indexAction()
{
   Zend_View_Helper_PaginationControl::setDefaultViewPartial('/pagination.phtml');
    $pages = new Application_Model_DbTable_Catalog();
    $adapter = $pages->getPagedCatalog(); //get adapter from model

    $page = $this->_getParam('page', 1);//a default of page 1 prevents possible unusual behavior when no page number is set.

    $paginator = new Zend_Paginator($adapter);
    $paginator->setItemCountPerPage('10');
    $paginator->setCurrentPageNumber($page);
    //$paginator->setView($this->view); This line is likely not needed
    $this->view->paginator = $paginator;
}

categoryAction()可能会像这样工作:

public function categoryAction()
{
    $page = $this->_getParam('page', 1);
    $id = intval($this->_getParam('id', 0));
    if ($id > 0) {
        $category = new Application_Model_DbTable_Categories();
        $adapter = $category->getPagedCatagories($id);
        //same as above
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage('10');
        $paginator->setCurrentPageNumber($page);
        //$paginator->setView($this->view); This line is likely not needed, unless you have multiple view objects.
        $this->view->paginator = $paginator;
    } else {
        //do some other stuff...
    }
}

如果你发疯了并且想使用自己的映射器类来建立分页器适配器,你可以通过覆盖来扩展适配器类getItems(),例如:

<?php

class Music_Model_Paginator_Track extends Zend_Paginator_Adapter_DbTableSelect
{
    //override getItems()
    public function getItems($offset, $itemCountPerPage)
    {
        $rows = parent::getItems($offset, $itemCountPerPage);

        $albums = array();
        foreach ($rows as $row) {
            $album = new Music_Model_Mapper_Track();//use this class to turn each $row into an object                
            $albums[] = $album->createEntity($row); //build the new entity objects
        }
        //returns an array of entity objects to the paginator
        return $albums;
    }
}

希望这可以帮助。

于 2013-02-01T09:06:24.880 回答