Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get PagerDefault queries to work correctly with Drupal 7?

I'm running the following code:

$query = db_select('taxonomy_index', 'ti')
  ->fields('ti', array('nid'))
  ->condition('ti.tid', $term->tid)
  ->condition('n.status', 1);

$query->join('node', 'n', 'n.nid = ti.nid');

$query->extend('PagerDefault')->limit(2);

$nids = $query->execute()->fetchCol();

but the pager does not work: every item from the query is returned, as if the call to PagerDefault is completely ignored. I am outputting theme('pager') further down in the output so that's not the problem.

This is not the only example of this failure that I have, in several other projects similar queries also bring back the full number of results every time.

I've read all the documentation, it seems to work sometimes and not other times. Anyone got any ideas?

Cheers

like image 599
Clive Avatar asked Oct 03 '11 15:10

Clive


2 Answers

It is not relevant when you call extend().

The only thing that is important is that you use the new object returned by extend(). The reason for this is that extend() creates a new object which wrappes the current object (Decorator pattern).

So, you need to use $query = $query->extend('PagerDefault'), like you do in your answer (combined with other calls).

like image 68
Berdir Avatar answered Sep 29 '22 01:09

Berdir


It is not working because you have to do

$query = $query->extend('PagerDefault')->limit(2);
like image 29
Christophe Avatar answered Sep 28 '22 23:09

Christophe