好吧,你不會將查詢參數發送到分頁程序,你會送他們到paginator adapter。然後,選定的適配器將執行所需的任何操作... 數據庫查詢或解析數組。 ..然後,您將數據庫適配器傳遞給paginator。
這裏是一個控制器的動作,要麼建立一個分頁程序或重定向基於一個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傳回到網址。
希望這會有所幫助。