Esempio n. 1
0
 public function testWithCacheDisabled()
 {
     $this->_paginator->setCacheEnabled(false);
     $this->_paginator->setCurrentPageNumber(1)->getCurrentItems();
     $cachedPageItems = $this->_paginator->getPageItemCache();
     $expected = new \ArrayIterator(range(1, 10));
     $this->assertEquals(array(), $cachedPageItems);
     $pageItems = $this->_paginator->getCurrentItems();
     $this->assertEquals($expected, $pageItems);
 }
Esempio n. 2
0
 /**
  * Retorna vários registros
  *
  * @param string|array $where  OPTIONAL An SQL WHERE clause
  * @param string|array $order  OPTIONAL An SQL ORDER clause.
  * @param int          $count  OPTIONAL An SQL LIMIT count.
  * @param int          $offset OPTIONAL An SQL LIMIT offset.
  *
  * @return array|null Lista de registros ou nulo se não localizar nenhum
  */
 public function fetchAll($where = null, $order = null, $count = null, $offset = null)
 {
     // Cria a assinatura da consulta
     if ($where instanceof Zend_Db_Select) {
         $md5 = md5($where->assemble());
     } else {
         $md5 = md5(var_export($this->showDeleted, true) . var_export($this->usePaginator, true) . var_export($where, true) . var_export($order, true) . var_export($count, true) . var_export($offset, true));
     }
     // Verifica se tem no cache
     // o Zend_Paginator precisa do Zend_Paginator_Adapter_DbSelect para acessar o cache
     if ($this->getUseCache() && !$this->getUsePaginator() && $this->getCache()->test($md5)) {
         return $this->getCache()->load($md5);
     } else {
         // Define a consulta
         if ($where instanceof Zend_Db_Select) {
             $select = $where;
         } else {
             $select = $this->getSelect($where, $order, $count, $offset);
         }
         // Verifica se deve usar o Paginator
         if ($this->getUsePaginator()) {
             $fetchAll = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
             // Verifica se deve usar o cache
             if ($this->getUseCache()) {
                 $fetchAll->setCacheEnabled(true)->setCache($this->getCache());
             }
             // Configura o paginator
             $fetchAll->setPageRange($this->getPaginator()->getPageRange());
             $fetchAll->setCurrentPageNumber($this->getPaginator()->getCurrentPageNumber());
             $fetchAll->setItemCountPerPage($this->getPaginator()->getItemCountPerPage());
         } else {
             // Recupera os registros do banco de dados
             $fetchAll = $this->getTableGateway()->fetchAll($select);
             // Verifica se foi localizado algum registro
             if (!is_null($fetchAll) && count($fetchAll) > 0) {
                 // Passa o $fetch para array para poder incluir campos extras
                 $fetchAll = $fetchAll->toArray();
                 // Verifica se deve adiciopnar campos extras
                 $fetchAll = $this->getFetchAllExtraFields($fetchAll);
             } else {
                 $fetchAll = null;
             }
             // Grava a consulta no cache
             if ($this->getUseCache()) {
                 $this->getCache()->save($fetchAll, $md5);
             }
         }
         // Some garbage collection
         unset($select);
         // retorna o resultado da consulta
         return $fetchAll;
     }
 }