Hi,

I'm trying to migrate a bunch of field collections between two sites that are fairly identical.

I have successfully used migrate_d2d for the core of the data, but of course field collections are not migrated automatically.

I wrote this class, which kind of works, as it imports the field collections, but it doesn't relate them to the proper nodes. I can see them with Views, seemingly with the correct values, but they're not related to any node.

The migration appears normally in the dashboard, and I can configure the mappings via the UI, then execute the thing with drush.

I understand something must be wrong with my class, but fail to see what.

class RandoDistanceMigration extends Migration {
  public function __construct($arguments) {
    parent::__construct($arguments);

    $this->description = t('Migration of Rando field collctions.');
    $this->dependencies = array('8d2922d10Noderandonn_e');

    $this->bdd = Database::getConnection('prod', 'default');
    $query = $this->bdd->select('field_data_field_distance', 'd');
    $query->fields('d', array('entity_id', 'field_distance_value'));
    $query->leftJoin('node', 'n', 'd.entity_id = n.nid');    
    $query->leftJoin('field_data_field_distance_prefix', 'p', 'p.entity_id = n.nid');
    $query->leftJoin('field_data_field_distance_value2', 'v', 'v.entity_id = n.nid');
    
    $query->fields('p', array('field_distance_prefix_tid'));    
    $query->fields('v', array('field_distance_value2_value'));    
    
    $query->condition('d.bundle', 'randonn_e', '=');    
    
    
    $this->source = new MigrateSourceSQL($query, array(), NULL, array('map_joinable' => FALSE));

    $this->destination = new MigrateDestinationFieldCollection(
      'field_distance', array('host_entity_type' => 'node'));

    $this->map = new MigrateSQLMap($this->machineName, array(
      'field_distance_value' => array(
        'type' => 'int',
        'length' => 11,
        'not null' => true,
      ),
        ), MigrateDestinationFieldCollection::getKeySchema()
    );

    $this->addFieldMapping('entity_id', 'nid')->sourceMigration('8d2922d10Noderandonn_e');
    
    $this->addFieldMapping('field_distance_prefix', 'field_distance_prefix');
    $this->addFieldMapping('field_distance_value2', 'field_distance_value2');
  }
}

It feels pretty close to OK, so I think just a little hint could be enough to make it work as intended.

Side question : Is there a way to test this kind of code on a small bunch of data ? I tried with the 'idlist' drush parameter but of course it doesn't work as the collections don't have an id of their own. Now I'm stuck to restoring the database each time it fails, which is very awkward.

Comments

Countzero created an issue. See original summary.

Countzero’s picture

Status: Active » Closed (won't fix)

Never mind. The whole thing is too messy and I gave up. BTW, there are several other issues in the queue about the same subject :

https://www.drupal.org/node/2502481
https://www.drupal.org/node/2558803