function recipe_update_7208 in Recipe 7.2
Move ingredient references in {recipe_node_ingredient} into the new field.
File
- ./
recipe.install, line 672 - Install, update and uninstall functions for the recipe module.
Code
function recipe_update_7208(&$sandbox) {
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current_nid'] = 0;
$sandbox['max'] = db_query('SELECT COUNT(DISTINCT id) FROM {recipe_node_ingredient} WHERE ingredient_id IS NOT NULL')
->fetchField();
}
$query = db_select('recipe_node_ingredient', 'r');
$query
->join('node', 'n', 'r.nid = n.nid');
$query
->fields('r', array(
'nid',
'unit_key',
'quantity',
'ingredient_id',
'note',
))
->fields('n', array(
'vid',
))
->condition('r.nid', $sandbox['current_nid'], '>')
->isNotNull('ingredient_id')
->orderBy('nid', 'ASC')
->range(0, 100);
$ingredient_references = $query
->execute();
// Track the last delta given to a node's field value.
// {recipe_node_ingredient}.weight can't be copied over since it may have
// duplicate values, which would violate the field's primary index unique
// integrity.
$last_deltas = array();
foreach ($ingredient_references as $ingredient_reference) {
$nid = $ingredient_reference->nid;
if (isset($last_deltas[$nid])) {
$last_deltas[$nid]++;
}
else {
$last_deltas[$nid] = 0;
}
db_insert('field_data_recipe_ingredient')
->fields(array(
'entity_type' => 'node',
'bundle' => 'recipe',
'entity_id' => $nid,
'revision_id' => $ingredient_reference->vid,
'language' => LANGUAGE_NONE,
'delta' => $last_deltas[$nid],
'recipe_ingredient_iid' => $ingredient_reference->ingredient_id,
'recipe_ingredient_quantity' => $ingredient_reference->quantity,
'recipe_ingredient_unit_key' => $ingredient_reference->unit_key,
'recipe_ingredient_note' => $ingredient_reference->note,
))
->execute();
$sandbox['progress']++;
$sandbox['current_nid'] = $ingredient_reference->nid;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}