/** * @param int $dataFormat * @param null $pdoArgs * @return object * @throws \Exception */ public function build($dataFormat = \PDO::FETCH_ASSOC, $pdoArgs = null) { $pager = array('pages' => array(), 'activePage' => $this->currentPage); //get data and total records from db $offset = ($this->currentPage - 1) * $this->pageSize; if ($this->rawQuery) { $query = "{$this->rawQuery} LIMIT {$offset}, {$this->pageSize}"; $this->rowCount = $this->db->execute()->rowCount(); $this->data = $this->query($query)->execute()->getResultSet($dataFormat, $pdoArgs); } else { $query = $this->db->getGeneratedQuery(false); $this->data = $this->db->limit($this->pageSize, $offset)->execute()->getResultSet($dataFormat, $pdoArgs); $this->rowCount = $this->db->query($query)->execute()->rowCount(); } //build paging data $last = $this->totalPages = ceil($this->rowCount / $this->pageSize); //last page $start = $this->currentPage - $this->range > 0 ? $this->currentPage - $this->range : 1; $end = $this->currentPage + $this->range < $last ? $this->currentPage + $this->range : $last; //go to first page if ($start > 1) { $pager['head'] = 1; } //pages in-between for ($page = $start; $page <= $end; $page++) { $pager['activePage'] = $this->currentPage == $page ? $page : $pager['activePage']; $pager['pages'][] = $page; } //go to last page if ($end < $last) { $pager['tail'] = $last; } //prev page if ($this->currentPage > 1) { $pager['previous'] = $this->currentPage - 1; } //next page if ($this->currentPage < $last) { $pager['next'] = $this->currentPage + 1; } $pager['data'] = $this->data; $pager['totalPages'] = $this->getTotalPages(); $pager['totalRecords'] = $this->getTotalRecords(); return (object) $pager; }
require_once 'Zend/Loader/Autoloader.php'; $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->setFallbackAutoloader(true); require_once 'Product.php'; require_once 'User.php'; $zendDb = Zend_Db::factory('Pdo_Sqlite', array('dbname' => APP_PATH . '/data/test.db')); function setupTestDb($db) { $create = array('CREATE TABLE IF NOT EXISTS products (id INTEGER PRIMARY KEY, name TEXT, price REAL, on_sale INTEGER DEFAULT 0, user_id INTEGER, created_on TEXT);', 'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT, email TEXT, password TEXT);'); foreach ($create as $sql) { $db->exec($sql); } } setupTestDb($zendDb); // simple db takes a Zend_Db instance and either a writable path or Zend_Cache instance $db = new SimpleDb($zendDb, APP_PATH . '/data'); $db->addClasses('Product', 'User'); // inform simpledb of our model classes (what about a dir scanner?) // you can access tables without model classes, results will be SimpleDb_Item instances $user = $db->users->new(array('username' => 'jim', 'email' => '*****@*****.**')); print 'Saving ' . $user . ' to database...<br />'; $user->save(); for ($i = 1; $i < 5; $i++) { $product = $db->products->new(); $product->name = 'Test Product ' . $i; $product->user_id = $user->id; $product->price = (double) ($i . '.99'); $product->on_sale = $i % 2 ? 1 : 0; $product->save(); } foreach ($db->products as $product) {
public function __destruct() { parent::__destruct(); }