【问题标题】:Zend_Paginator for CategoriesZend_Paginator 分类
【发布时间】:2013-02-01 17:06:24
【问题描述】:

我在我的索引操作中使用了 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:-(, 附言对不起我的英语不好

【问题讨论】:

    标签: zend-framework zend-paginator


    【解决方案1】:

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

    分页从您的数据库查询开始。您正在使用 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 将自动为查询提供$limit$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;
        }
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      相关资源
      最近更新 更多