#!/usr/bin/php 'field_collection'))) as $field_name) { // Find field collection items with duplicate revision ids $query = db_select("field_revision_{$field_name}", 't') ->fields('t', array("{$field_name}_revision_id")) ->groupBy("t.{$field_name}_revision_id"); $query->addExpression("COUNT(t.{$field_name}_revision_id)", 'vidcount'); $query->havingCondition('vidcount', 2, ">="); $vids = $query->execute()->fetchCol(); // Get the fields in this field collection $inner_fields = field_read_instances(array('entity_type' => 'field_collection_item', 'bundle' => $field_name)); // Each revision ID that occurs more than once foreach ($vids as $field_collection_revision_id) { // Get the values to copy $values = array(); foreach ($inner_fields as $copy_from) { $values[$copy_from['field_name']] = db_query("SELECT * FROM field_data_{$copy_from['field_name']} WHERE revision_id = $field_collection_revision_id")->fetchAll(); } // Find every instance of this revision ID $copies = db_select("field_revision_{$field_name}", 't') ->fields('t') ->condition("{$field_name}_revision_id", "$field_collection_revision_id") ->execute() ->fetchAllAssoc("revision_id"); $first_copy = reset($copies); $field_collection_item_id = $first_copy->{"{$field_name}_value"}; // Find new revisions of this field collection item that were made after // revisions were enabled in update_7003 $new_revisions = db_select("field_revision_{$field_name}", 't') ->fields('t') ->condition("{$field_name}_revision_id", "$field_collection_revision_id", "<>") ->condition("{$field_name}_value", $field_collection_item_id) ->execute() ->fetchAllAssoc("revision_id"); // Intentionally skip the first instance as it doesn't need to be modified while (FALSE !== ($row_to_replace = next($copies))) { var_dump($row_to_replace); exit(); } //exit(); } }