Posted By

wizonesolutions on 01/01/12


Tagged

batch drupal api


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wizonesolutions


Drupal Batch API for hook_update_N()


 / Published in: PHP
 

URL: http://api.drupal.org/api/drupal/developer--hooks--install.php/function/hook_update_N/6

This is the Batch API example shown on http://api.drupal.org/api/drupal/developer--hooks--install.php/function/hookupdateN/6.

  1. <?php
  2. // However, for more complex operations that may take a long time,
  3. // you may hook into Batch API as in the following example.
  4. $ret = array();
  5.  
  6. // Update 3 users at a time to have an exclamation point after their names.
  7. // (They're really happy that we can do batch API in this hook!)
  8. if (!isset($sandbox['progress'])) {
  9. $sandbox['progress'] = 0;
  10. $sandbox['current_uid'] = 0;
  11. // We'll -1 to disregard the uid 0...
  12. $sandbox['max'] = db_result(db_query('SELECT COUNT(DISTINCT uid) FROM {users}')) - 1;
  13. }
  14.  
  15. $users = db_query_range("SELECT uid, name FROM {users} WHERE uid > %d ORDER BY uid ASC", $sandbox['current_uid'], 0, 3);
  16. while ($user = db_fetch_object($users)) {
  17. $user->name .= '!';
  18. $ret[] = update_sql("UPDATE {users} SET name = '$user->name' WHERE uid = $user->uid");
  19.  
  20. $sandbox['progress']++;
  21. $sandbox['current_uid'] = $user->uid;
  22. }
  23.  
  24. $ret['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  25.  
  26. return $ret;
  27. ?>

Report this snippet  

You need to login to post a comment.