1

I am using one custom module, which is getting data from the database, with the following code.

function rs_get_doors() {
  $doors = array();
  $doors_nids =  db_select('node', 'n')
  $doors_nids = $door_nids->extend('PagerDefault')->limit(10);
    ->condition('n.type', 'vivarium_door')
    ->fields('n', array('nid'))
    ->orderBy('n.title', 'ASC')
    ->execute()
    ->fetchCol();

  foreach ($doors_nids as $nid) {
    $wrapper = entity_metadata_wrapper('node', $nid);
    $doors[$nid] = array(
      'name' => $wrapper->title->value(),
      'fee_nid' => $wrapper->field_fee_category->value()->nid,
    );
  }

  uasort($doors, 'rs_doors_natural_sort');

  return $doors;
}

it is getting thousands of nodes at time in a single page and it takes too much time to load.

So I think I need to paginate the query. How can I achieve this?

0
$doors_nids = db_select('node', 'n')

->condition('n.type', 'vivarium_door')

->fields('n', array('nid'))

->orderBy('n.title', 'ASC')

->extend('PagerDefault')->limit(10)

->execute()

->fetchCol();

Hopes this will helps you.

|improve this answer|||||
  • @Smalution- thanks for your response i will check with this and come back to you – DINE Feb 8 '16 at 8:18
  • @Smalution- it is showing like this error Parse error: syntax error, unexpected '$doors_nids' (T_VARIABLE) – DINE Feb 8 '16 at 9:12
  • can you see my edits ...after modification of code..my full code has shown above.. – DINE Feb 8 '16 at 9:20
  • updated answer you can check now. – DRUPWAY Feb 8 '16 at 10:19
  • @ Smalution - thanks..i will check and get back to you – DINE Feb 8 '16 at 11:10
0

You are using the wrong method: It's not limit(), but range() that you should use.

  $query = db_select('node', 'n')
    ->condition('n.type', 'vivarium_door')
    ->fields('n', array('nid'))
    ->orderBy('n.title', 'ASC')
    ->range(0, 10);

  $result = $query->execute(); // Add here the call to fetchCol().

Notice that dynamic queries are slower than static queries; you should use them when you want other modules to dynamically alter your query. In the other cases, you should use static queries (i.e. use db_query() or db_query_range()).

$query = db_query_range("SELECT n.nid FROM {node} n WHERE n.type = 'vivarium_door' ORDER BY n.title ASC", 0, 10);

Reference

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.