Zend Framework:将GET参数传递给paginator

时间:2013-01-24 17:49:12

标签: zend-framework zend-paginator

我正在尝试完成搜索表单,其中一个部分是分页。问题是下一个: 我不知道如何将GET参数传递给paginator链接(例如:/ page / 2?search = query)。 我在application / scripts目录中创建了“controls.phtml”,并在Bootsrap中添加了

Zend_View_Helper_PaginationControl::setDefaultViewPartial('controls.phtml');

并在视图中呈现分页器:

echo $this->paginationControl($this->paginator, 'Elastic', 'controls.phtml');

有人可以编写我需要做的逐步操作来决定这个问题吗? (ZF我只是第二天)

2 个答案:

答案 0 :(得分:0)

我会在Controller上做更多的事情,并将$paginator本身传递给视图。在你的控制器中有这样的东西:

Zend_Paginator::setDefaultScrollingStyle("Elastic");
Zend_View_Helper_PaginationControl::setDefaultViewPartial("controls.phtml");

$paginator = Zend_Paginator::factory($result);
$paginator->setItemCountPerPage(5);
$paginator->setPageRange(5);
//this is where you collect the GET param and use as parameter.
$paginator->setCurrentPageNumber((int) $this->getRequest()->getParam("page", 1));

$this->view->paginator = $paginator; //pass it from Controller into View

然后在查看代码中:

<?= $this->paginator; ?>

答案 1 :(得分:0)

好的,你不会将查询参数发送给paginator,你将把它们发送到paginator adapter。然后,所选适配器将执行所需的任何操作... 数据库查询或解析数组。 ..然后您将数据库适配器传递到分页器。

这是一个控制器操作,可以构建一个分页器,也可以根据获取查询重定向:

public function displayAction()
    {
        //get all parameters from requested url
        $request = $this->getRequest()->getParams();
        //build switch based GET query
        switch ($request) {
            //if ?query=value
            case (isset($request['query'])):
                //model to use for database query
                $model = new Music_Model_Mapper_Track();
                //paginator adapter to use
                $adapter = $model->fetchPagedTracks(null, $request['query']);
                //intiate new paginator
                $paginator = new Zend_Paginator($adapter);
                //provide paginator page values
                $paginator->setItemCountPerPage(20)->setPageRange(5);
                //?page=1 is default
                $page = $this->getRequest()->getParam('page', 1);
                //current page number
                $paginator->setCurrentPageNumber($page);
                //pass paginator to view
                $this->view->paginator = $paginator;
                break;
            //if ?id=value
            case (isset($request['id'])):
                $this->_forward('artist', null, null, array('id' => $request['id']));
                break;
            //if neither query value present. Should never get here.
            default:
                $adapter = $model->fetchPagedTracks();
                //[...]
                break;
        }
    }

我使用修改后的适配器作为我的paginator适配器。它从DbTableSelect适配器扩展而来,只需将映射器替换为数据库表:

class Music_Model_Paginator_Track extends Zend_Paginator_Adapter_DbTableSelect
{
    /**
     * Override getItems() to use mapper model instead of database table
     */
    public function getItems($offset, $itemCountPerPage)
    {
        $rows = parent::getItems($offset, $itemCountPerPage);

        $albums = array();
        foreach ($rows as $row) {
            $album = new Music_Model_Mapper_Track();
            $albums[] = $album->createEntity($row);
        }
        return $albums;
    }
}

在该映射器中,我使用paginator适配器的方式与使用默认适配器的方式完全相同:

/**
     * Creates Zend_Paginator_Adapter_DbTableSelect object,
     * based on artist_id or query string.
     *
     * @param string $id
     * @param string $query
     * @return Music_Model_Paginator_Track
     */
    public function fetchPagedTracks($id = null, $query = null)
    {
        //if $id is not null where=$id
        if (!is_null($id)) {
            $select = $this->getGateway()->select();
            $select->where('track.artist_id = ?', $id);
        }
        //if $query is not null join on the artist table and query track title and artist name for match.
        if (!is_null($query)) {
            $select = $this->getGateway()
                ->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
            $select->setIntegrityCheck(false);
            $select->join('artist', 'artist.id = track.artist_id');
            $select->where("track.title LIKE '%$query%'");
            $select->orWhere("artist.name LIKE '%$query%'");
        }
        //default select query if $id and $query are null, appended to query otherwise.
        $select->order('track.artist_id ASC');
        $select->order('track.album_id ASC');
        $select->order('track.track_number ASC');
        //instantiate paginator adapter
        //$adapter = new Zend_Paginator_Adapter_DbTableSelect() would also work
        $adapter = new Music_Model_Paginator_Track($select);
        //return paginator adapter
        return $adapter;
    }

最后这个观点没什么特别的:

<div><?php
echo $this->paginationControl(
        $this->paginator, 'Sliding', '_paginatorControl.phtml'
)
?></div>
//[..]

对于某些适配器(在某些情况下),您可能会将Zend_RegistryZend_Session的查询值保留为传递回网址。

希望这有帮助。