开发者

Using Zend_Paginator without Select Adapter

I want to use Zend_Paginator but Zend_Paginator requires Zend_Db_Select as input parameter.

My SQL query is some how a little complicated making it so difficult to impleme开发者_运维百科nt with Zend_Db_Select.

Can i use Zend_Paginator with plain SQL query?


Yes you can, From the ZF docs:

The primary design goals of Zend_Paginator are as follows:

  • Paginate arbitrary data, not just relational databases
  • Loosely couple Zend_Paginator to other Zend Framework components so that users who wish to use it independently of Zend_View, Zend_Db, etc. can do so

This page gives examples of the kind of the different adaptors available to Zend_Paginator.

For example, to create a paginator using an array (which could be a database result set) you can use the Array adapter:

$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($array));


I have had the same problem, my SQL queries are a little complicated and work with Zend_Db_Select it does very complicated, but I got one solution through implementing Zend_Paginator_Adapter_Interface, which is easy.

//$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()

So, now you have to ensure that you catch the 'page' parameter and calculated the LIMIT for the current page to add in your SQL query, like:

$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);

And as in my case, I've got about thirty variables to make the SQL. I pass these by GET, then my navigation controls looks like this:

<?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; ?>">
              &lt;Anterior
          </a> 
        <?php else: ?>
          <span class="disabled rounded">&lt; 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&gt;
          </a>
        <?php else: ?>
          <span class="disabled rounded">Next &gt;</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; ?>

And that's all, it works fine!
I hope this will be useful for someone
Greetings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜