好的,您不会将查询参数发送到分页器,而是将它们发送到分页器适配器。然后选定的适配器将执行所需的任何操作...数据库查询或解析数组。.. 然后您将数据库适配器传递给分页器。
这是一个控制器操作,它可以构建分页器或基于 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_Registry或Zend_Session将查询值持久化以传递回 url。
希望这可以帮助。