Skip to content

Instantly share code, notes, and snippets.

@ajbonner
Last active December 19, 2015 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajbonner/5900045 to your computer and use it in GitHub Desktop.
Save ajbonner/5900045 to your computer and use it in GitHub Desktop.
Get the number of rows matching a select query with Zend_Db
<?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