Drupal - Can we get full count of results from a query that is extended by PagerDefault
With the existing pager class I don't think you can get that data without running the count query again. The total count is queried directly and saved to a local var before being passed off to pager_default_initialize()
...it's not saved anywhere.
One option would be to extend the default class and use that for your pager queries. This is un-tested but I think it'll do the trick
class MyPager extends PagerDefault {
/**
* Overrides PagerDefault::execute().
*/
public function execute() {
// Add convenience tag to mark that this is an extended query. We have to
// do this in the constructor to ensure that it is set before preExecute()
// gets called.
if (!$this->preExecute($this)) {
return NULL;
}
// A NULL limit is the "kill switch" for pager queries.
if (empty($this->limit)) {
return;
}
$this->ensureElement();
// Get the total items from our new statically cached function.
$total_items = $this->getTotalItemCount();
$current_page = pager_default_initialize($total_items, $this->limit, $this->element);
$this->range($current_page * $this->limit, $this->limit);
// Now that we've added our pager-based range instructions, run the query normally.
return $this->query->execute();
}
/**
* Cache the results of PagerDefault::getCountQuery().
*/
public function getTotalItemCount() {
static $total_items = NULL;
if ($total_items === NULL) {
$total_items = $this->getCountQuery()->execute()->fetchField();
}
return $total_items;
}
}
You could then construct a query like the following
$query = db_select('table', 't')
->fields('t')
->condition('foo', 'bar')
->extend('MyPager');
$current_items = $query->execute();
$total_items = $query->getTotalItemCount();
There might be an easier way, but I just can't see it.