You are here

function weight_update_7206 in Weight 7.2

Move existing node weights to weight_weights from field_data_weight.

See also

weight_update_7201().

File

./weight.install, line 347

Code

function weight_update_7206(&$sandbox) {
  if (db_table_exists('field_data_weight')) {
    if (!isset($sandbox['progress'])) {
      $sandbox['progress'] = 0;
      $sandbox['last'] = 0;
      $sandbox['max'] = db_query("SELECT COUNT(entity_id) FROM {field_data_weight}\n        WHERE language=:lang", array(
        ':lang' => LANGUAGE_NONE,
      ))
        ->fetchField();
    }
    $query = db_select('field_data_weight', 'fw');
    $query
      ->fields('fw', array(
      'entity_id',
      'weight_value',
    ))
      ->condition('fw.language', LANGUAGE_NONE)
      ->condition('fw.entity_id', $sandbox['last'], '>')
      ->orderBy('fw.entity_id')
      ->range(0, 200);
    $nodes = $query
      ->execute();
    foreach ($nodes as $node) {
      db_update('weight_weights')
        ->fields(array(
        'entity_id' => $node->entity_id,
        'entity_type' => 'node',
        'weight' => $node->weight_value,
      ))
        ->condition('entity_id', $node->entity_id)
        ->execute();
      $sandbox['progress']++;
      $sandbox['last'] = $node->entity_id;
    }
    $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  }
}