0

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

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

并在视图中呈现分页器:

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

有人可以写出我需要做的分步操作来解决这个问题吗?(与采埃孚我只有第 2 天)

4

2 回答 2

0

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

这是一个控制器操作,它可以构建分页器或基于 get 查询进行重定向:

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

我为分页器适配器使用了修改后的适配器。它是从 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;
    }
}

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

/**
     * 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将查询值持久化以传递回 url。

希望这可以帮助。

于 2013-01-25T09:42:27.240 回答
0

我会在控制器上做更多的事情,只是将$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; ?>
于 2013-01-24T21:34:40.900 回答