使用したいのですZend_Paginator
が、入力パラメータとしてZend_Paginator
必要です。Zend_Db_Select
私の SQL クエリは少し複雑で、 で実装するのが非常に困難Zend_Db_Select
です。
Zend_Paginator
プレーン SQL クエリで使用できますか?
使用したいのですZend_Paginator
が、入力パラメータとしてZend_Paginator
必要です。Zend_Db_Select
私の SQL クエリは少し複雑で、 で実装するのが非常に困難Zend_Db_Select
です。
Zend_Paginator
プレーン SQL クエリで使用できますか?
はい、できます。ZF ドキュメントから:
Zend_Paginator の主な設計目標は次のとおりです。
- リレーショナル データベースだけでなく、任意のデータをページ分割する
- Zend_Paginator を他の Zend Framework コンポーネントに疎結合して、ユーザーが Zend_View や Zend_Db などから独立して使用できるようにします。
このページでは、 で使用できるさまざまなアダプタの種類の例を示しますZend_Paginator
。
たとえば、配列 (データベースの結果セットである可能性があります) を使用してページネーターを作成するには、配列アダプターを使用できます。
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($array));
私は同じ問題を抱えていました。私の SQL クエリは少し複雑で、Zend_Db_Select を使用すると非常に複雑になりますが、Zend_Paginator_Adapter_Interface を実装することで 1 つの解決策が得られました。これは簡単です。
//$data, has a $rowSet() and a counter variable with the result of ('SELECT FOUND_ROWS() as count')
//rowSet come from $rowSet = $this->_db->fetchAll($sql);//Default adapter
$adapter = new Fidelizacion_Model_AdapterVisitasClientes($data);
$this->_clientePager = new Zend_Paginator($adapter);
//My own adapter
/**
* @author Jose
*/
class Fidelizacion_Model_PaginatorVisitasClientes implements Zend_Paginator_Adapter_Interface
{
/**
* Array
*
* @var array
*/
protected $_array = null;
/**
* Item count
*
* @var integer
*/
protected $_count = null;
/**
* Constructor.
*
* @param array $array Array to paginate
*/
public function __construct($array)
{
$this->_array = $array['rowSet'];
$this->_count = $array['count'];
}
public function getItems($offset, $itemCountPerPage)
{
$clientes = array();
if($this->_array){
foreach ($this->_array as $row) {
$clpo = new Clientes_Model_ClienteMin();
$clpo->setId($row['id']);
$clpo->setNombre($row['nombre']);
$clpo->setNumeroDocumento($row['numero_documento']);
$clpo->setTelefonoContacto($row['telefono_contacto']);
$clpo->setLocalidad($row['localidad']);
$clpo->setPersonaContacto($row['nombre_corto']);
$clientes[] = $clpo;
$clpo = null;
}
}
return $clientes;
}
/**
* Returns the total number of rows in the array.
*
* @return integer
*/
public function count()
{
return $this->_count;
}
}
//As you can see with that you can encapsulate in objects and set the conunt() method with the value of FOUND_ROWS()
したがって、次のように、「ページ」パラメーターをキャッチし、現在のページの LIMIT を計算して SQL クエリに追加する必要があります。
$sql .= ' LIMIT '.(((ITEMS_PER_PAGE * (int)$this->_getParam('page',1)) - ITEMS_PER_PAGE)).','.(ITEMS_PER_PAGE);
$set = $this->_getClientesDao()->getClientesFiltroVisitas($sql);
$paginator = $this->_getClientesPager($set);//returns the pager instantie with my own adapter
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(ITEMS_PER_PAGE);
$paginator->setPageRange(5);
そして、私の場合と同様に、SQL を作成するために約 30 個の変数があります。これらを GET で渡すと、ナビゲーション コントロールは次のようになります。
<?php if ($this->pageCount): ?>
<div class="pagination_control">
<a class="ends rounded" href="<?php echo $this->url() . '?' . $_SERVER['QUERY_STRING'] . '&page=' . $this->first ;?>">
Primero
</a>
<?php if (isset($this->previous)): ?>
<a class="movement rounded" href="<?php echo $this->url() . '?' . $_SERVER['QUERY_STRING'] . '&page=' . $this->previous; ?>">
<Anterior
</a>
<?php else: ?>
<span class="disabled rounded">< Previous</span>
<?php endif; ?>
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a class="page square rounded" href="<?php echo $this->url() . '?' . $_SERVER['QUERY_STRING'] . '&page=' .$page; ?>">
<?php echo $page; ?>
</a>
<?php else: ?>
<?php echo "<span class='current square rounded'>$page</span>" ?>
<?php endif; ?>
<?php endforeach; ?>
<?php if (isset($this->next)): ?>
<a class="movement rounded" href="<?php echo $this->url() . '?' . $_SERVER['QUERY_STRING'] . '&page=' . $this->next;?>">
Siguiente>
</a>
<?php else: ?>
<span class="disabled rounded">Next ></span>
<?php endif; ?>
<a class="ends rounded" href="<?php echo $this->url() . '?' . $_SERVER['QUERY_STRING'] . '&page=' .$this->last; ?>">
Ùltimo
</a>
<span class="pagecount">
Página <?php echo $this->current; ?> de <?php echo $this->pageCount; ?>
</span>
</div>
<?php endif; ?>
そしてそれだけです、それはうまくいきます!
これが誰かの役に立てば幸いです
。