Php 我的Drupal7批处理代码正在一次又一次地重复批处理的最后一次迭代。有人能帮我修一下吗?

Php 我的Drupal7批处理代码正在一次又一次地重复批处理的最后一次迭代。有人能帮我修一下吗?,php,drupal,drupal-7,Php,Drupal,Drupal 7,我正在尝试将数据从旧数据库移动到新数据库。我的代码目前没有写任何东西(这很好),但是读取会在最后一条记录上反复重复,即使$context['finished']设置为1。知道我做错了什么吗 <?php function ogamigrate_permission() { return array( 'migrate to oga2' => array( 'title' => t('OGA 1.x -> OGA 2.0 Data Migration'

我正在尝试将数据从旧数据库移动到新数据库。我的代码目前没有写任何东西(这很好),但是读取会在最后一条记录上反复重复,即使$context['finished']设置为1。知道我做错了什么吗

<?php
function ogamigrate_permission() {
  return array(
    'migrate to oga2' => array(
      'title' => t('OGA 1.x -> OGA 2.0 Data Migration'),
      'description' => t('Migrate data from the old site.'),
    ),
  );
}

function ogamigrate_menu() {
  $items['admin/ogamigrate'] = array(
    'page callback' => '_ogamigrate_batch',
    'access arguments' => array('migrate to oga2'),
  );

  $items['admin/ogamigrate/finished'] = array(
    'page callback' => '_ogamigrate_complete',
    'access arguments' => array('migrate to oga2'),
  );
  return $items;
}

function _ogamigrate_batch() {
  $batch = array(
    'title' => t('Migrating data from OGA 1'),
    'operations' => array(
      array('_ogamigrate_tags', array()),
      #array('my_function_2', array()),
   ),
    'finished' => '_ogamigrate_finished',
  );
  batch_set($batch);
  batch_process('admin/ogamigrate/finished');
}

function _ogamigrate_tags(&$context) {
  db_set_active('old');
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_tid'] = 0;
    $context['sandbox']['max'] = db_query('select max(tid) from {term_data} where vid in (3, 4, 6, 7, 10);')->fetchField();
  }

  error_log("migrating tid {$context['sandbox']['current_tid']} ({$context['finished']}");

  $limit = 5;

  $result = db_select('term_data')
  ->fields('term_data', array('tid', 'name', 'description'))
  ->condition('tid', $context['sandbox']['current_tid'], '>')
  ->condition('vid', array(3, 4, 6, 7, 10), 'in')
  ->orderBy('tid')
  ->range(0, $limit)
  ->execute();

  db_set_active('default');

  foreach ($result as $row) {
    #$node = node_load($row->nid, NULL, TRUE);
   error_log("Processing tid {$row->tid} / {$context['sandbox']['max']} ({$row->name})");
    $context['results'][] = $row->tid . ' : ' . $row->name;
    $context['sandbox']['progress']++;
    $context['sandbox']['current_tid'] = $row->tid;
    $context['message'] = $row->name;
  }

  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}

function _ogamigrate_finished($success, $results, $operations) {
  error_log('finished');
  if ($success) {
    $message = format_plural(count($results), 'One item processed.', '@count item processed.');
  }
  else {
    $message = t('Finished with an error.');
  }
  drupal_set_message($message);
  /*
  // Providing data for the redirected page is done through $_SESSION.
  foreach ($results as $result) {
    $items[] = t('Loaded node %title.', array('%title' => $result));
  }
  $_SESSION['my_batch_results'] = $items;
  */
}

function _ogamigrate_complete() {
  return "<p>Migration complete.</p>";
}

当然,我花了几个小时试图弄明白这一点,在我发布它的那一刻,我就弄明白我做错了什么

事情是这样的:每次我添加一条记录时,我都会增加“progress”,但由于我没有包括每一个分类词汇表,它实际上跳过了一些记录,这意味着“progress”的总和永远不会等于“max”,因为“max”是一个记录ID,而不仅仅是记录总数p