Php 在没有选择适配器的情况下使用Zend_Paginator

Php 在没有选择适配器的情况下使用Zend_Paginator,php,zend-framework,zend-paginator,Php,Zend Framework,Zend Paginator,我想使用Zend_Paginator但是Zend_Paginator需要Zend_Db\u Select作为输入参数 我的SQL查询有点复杂,使用Zend\u Db\u Select实现起来非常困难 我可以将Zend_Paginator与普通SQL查询一起使用吗?可以,从ZF文档: 的主要设计目标 Zend_Paginator如下所示: 分页任意数据,而不仅仅是关系数据库 将Zend_Paginator松耦合到其他 Zend框架组件,以便 希望独立使用它的用户 Zend_View、Zend_D

我想使用
Zend_Paginator
但是
Zend_Paginator
需要
Zend_Db\u Select
作为输入参数

我的SQL查询有点复杂,使用
Zend\u Db\u Select
实现起来非常困难


我可以将Zend_Paginator与普通SQL查询一起使用吗?

可以,从ZF文档:

的主要设计目标 Zend_Paginator如下所示:

  • 分页任意数据,而不仅仅是关系数据库
  • 将Zend_Paginator松耦合到其他 Zend框架组件,以便 希望独立使用它的用户 Zend_View、Zend_Db等都可以这样做
给出了可用于Zend_Paginator的不同适配器类型的示例

例如,要使用数组(可以是数据库结果集)创建分页器,可以使用数组适配器:

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

我也遇到过同样的问题,我的SQL查询有点复杂,使用Zend_Db_Select时非常复杂,但我通过实现Zend_Paginator_Adapter_接口得到了一个解决方案,这很简单

//$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()
因此,现在您必须确保捕获“page”参数,并计算要添加到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);
和我的例子一样,我有大约30个变量来生成SQL。我通过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; ?>">
              &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; ?>

以前的

如果我将它与数组适配器一起使用,我必须从DB获取所有结果,这不是一个好的方法。当然,这是真的,我只是提供了一个灵活性的示例。所以没有适合我的情况的适配器。是吗?您可以使用缓存来防止每次获取所有结果,如图所示,尽管我会查看您的SQL,因为Zend_Db非常灵活,您应该能够使用Zend_Db_Selective使其工作。因此,我将问另一个问题,将我的查询更改为Zend_Db_Selection对象。thx.)