Last active
December 19, 2015 04:49
-
-
Save ajbonner/5900045 to your computer and use it in GitHub Desktop.
Get the number of rows matching a select query with Zend_Db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ZdbCountExample | |
{ | |
public function doSelect() | |
{ | |
$pageSize = 1000; | |
$numPages = ceil($this->count($this->baseSelect()) / $pageSize); | |
for ($page = 0; $page <= $numPages; $page++) { | |
$data = $this->readPage($this->baseSelect(), $page, $pageSize); | |
} | |
} | |
protected function baseSelect() | |
{ | |
return $this->db | |
->select() | |
->from('a_table') | |
->columns(array('some', 'columns')); | |
} | |
protected function readPage($select, $page, $pageSize) | |
{ | |
$select->limitPage($page, $pageSize); | |
$rs = $this->db->query($select); | |
return $rs->fetchAll(); | |
} | |
protected function count(Zend_Db_Select $select) | |
{ | |
$select->reset('columns') | |
->columns(array('total_count' => new Zend_Db_Expr('count(*)'))); | |
$rs = $this->getReadConnection()->fetchRow($select); | |
return $rs['total_count']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment