![]() |
|
Snippets |
|
This is just a version of the nonHydratingPager for Propel, adapted for Doctrine.
<?php /* Designed to be compatible with sfDoctrinePager only accept raw sql queries instead * of a Doctrine_Query object and spit back a resultset which you most likely want to * fill an array with instead of the standard array of hydrated objects. * * This solution is based on Propel's by Noel Tarnoff, Oz Basarir, dev AT (NOSPAM) naturalcapitalDOTorg * * @author David Morales, davidmoralesmojica AT (NOSPAM) gmailDOTcom * * Typical usage scenario: * 1) build 2 query strings with common WHERE clause, one for count one for selecting the rows * 2) pass the queries in with page and maxPerPage into the constructor ( no need to ->init() ) * 3) iterate through your result set (array of values) * 4) sit back and watch the fun * * ex. * $results = new nonHydratingPager($query_select, $query_count, $page, $max); * * foreach( $results as $result ) * { * ... * } */ class nonHydratingPager extends sfPager { private $resultSet = null; public function __construct($query, $query_count, $page = 1, $maxPerPage = 25) { $this->setPage($page); $this->setMaxPerPage($maxPerPage); $rs = Doctrine_Manager::getInstance()->getCurrentConnection()->fetchAssoc($query_count); $this->setNbResults($rs[0]['count']); $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage())); $startIndex = (($this->getPage()) - 1) * $maxPerPage; $query .= ' LIMIT ' . $maxPerPage . ' OFFSET ' . $startIndex; $this->resultSet = Doctrine_Manager::getInstance()->getCurrentConnection()->fetchAssoc($query); } public function init() {} public function getResults() { return $this->resultSet; } protected function retrieveObject($offset) {} }
<?php /* Designed to be compatible with sfPropelPager only accept raw sql queries instead * of a criteria object and spit back a resultset which you most likely want to * fill an array with instead of the standard array of hydrated objects. * * @author Noel Tarnoff, Oz Basarir dev AT (NOSPAM) naturalcapitalDOTorg * * Typical usage scenario: * 1) build 2 query strings with common WHERE clause, one for count one for selecting the rows * 2) pass the queries in with page and maxPerPage into the constructor ( no need to ->init() ) * 3) iterate through your result set and build your array of values * 4) sit back and watch the fun * * ex. * $objPager = new nonHydratingPager($query_select, $query_count, $page, $max); * * $rs = $objPager->getResultSet(); * * $arrEntities = array(); * while ( $rs->next() ) { * $arrEntities[$] = array('field1'=>$rs->getString(1), * 'field2'=>$rs->getString(2), * 'field3'=>$rs->getString(3)); * } */ class nonHydratingPager extends sfPager { private $resultSet = null; public function __construct($query, $query_count, $page = 1, $maxPerPage = 25) { $this->setPage($page); $this->setMaxPerPage($maxPerPage); $con = Propel::getConnection(); $stmt = $con->prepareStatement($query_count); $rs = $stmt->executeQuery(ResultSet::FETCHMODE_NUM); $rs->next(); $this->setNbResults($rs->get(1)); $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage())); $startIndex = (($this->getPage()) - 1) * $maxPerPage; $query .= ' LIMIT ' . $maxPerPage . ' OFFSET ' . $startIndex; $stmt = $con->prepareStatement($query); $this->resultSet = $stmt->executeQuery(ResultSet::FETCHMODE_NUM); } public function init() {} public function getResults() { return $this->resultSet; } protected function retrieveObject($offset) {} }