Does anybody know how to use the migrate module for migrating relations? I can't find a solution for it.
relation module: http://drupal.org/project/relation

Comments

mikeryan’s picture

Project: Migrate » Migrate Extras
Version: 7.x-2.2 » 7.x-2.x-dev
Category: support » feature

As explained on the project page, support for contributed modules should go into the contrib module itself, or into Migrate Extras.

This has not previously been requested.

nerdcore’s picture

I am interested in the ability to migrate individual Relations (I have built my Relation Types manually).

It seems this is being looked into by BTMash on the Relation issues queue: #1053108: Upgrade path from *reference?

chromix’s picture

I'll be working on this over the next week. I'll report something when we have something.

slashrsm’s picture

Cross linking: #1606404: Migrate integration & upgrade from *reference

Linked issue includes a patch.

mikeryan’s picture

Status: Active » Closed (duplicate)

Closing this in favor of the Relation issue #1606404: Migrate integration & upgrade from *reference.

vlad.dancer’s picture

Issue summary: View changes

Just an example how to migrate relations.
Note: enable relation_migrate module to load relation_migrate.destination.inc or just include it inside yours.

/**
 * @file
 * Migration for relation entities.
 */

/**
 * Class DwRelationBaseImport.
 *
 * Base class for all relations.
 * The common thing to map two source fields for endpoints,
 * and later use them in prepare method.
 */
abstract class DwRelationBaseImport extends Migration {
  /**
   * {@inheritdoc}
   */
  public function getDestinationPk() {
    return MigrateDestinationRelation::getKeySchema();
  }

  abstract public function getInstanceName();

  /**
   * {@inheritdoc}
   */
  public function fields() {
    $fields = array();

    $fields['left_entity_id']  = t('First Endpoint Entity ID');
    $fields['right_entity_id'] = t('Seconds Endpoint Entity ID');

    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function __construct($arguments = array()) {
    parent::__construct($arguments);
    
    $this->query = $this->query();

    $this->source = new MigrateSourceSQL(
      $this->query,
      $this->fields(),
      NULL,
      array('map_joinable' => FALSE)
    );

    $this->map = $this->buildMap();

    $this->destination = new MigrateDestinationRelation(RELATION_MACHINE_NAME);

    $this->addFieldMapping('endpoints', NULL)->defaultValue(array());
    $this->addFieldMapping('left_entity_id', 'left_entity_id');
    $this->addFieldMapping('right_entity_id', 'right_entity_id');

    $this->addUnmigratedDestinations(array(
      'is_new',
      'path',
    ));
    
  }

  /**
   * Build map using class overrides.
   *
   * @return MigrateSQLMap
   *   An db-based MigrateMap.
   */
  private function buildMap() {
    return new MigrateSQLMap($this->getInstanceName(),
      $this->getSourcePK(),
      $this->getDestinationPK()
    );
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow($row) {
    if (parent::prepareRow($row) === FALSE) {
      return FALSE;
    }

    $row->right_entity_id = trim($row->right_entity_id);
    $row->left_entity_id  = trim($row->left_entity_id);

    if (empty($row->right_entity_id) || empty($row->left_entity_id)) {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function prepare(stdClass $relation, stdClass $source_row) {
    $left_entity_id  = $relation->left_entity_id;
    $right_entity_id = $relation->right_entity_id;

    unset($relation->left_entity_id);
    unset($relation->right_entity_id);

    if (empty($left_entity_id) || empty($right_entity_id)) {
      $this->queueMessage(
        t('Import item failed <br>left entity id: %left,<br>right entity id: %right)', array(
          '%left' => var_export($left_entity_id, TRUE),
          '%right' => var_export($right_entity_id, TRUE),
        )),
        MigrationBase::MESSAGE_WARNING
      );

      // Ignore saving of a relation.
      $relation->rid    = FALSE;
      $relation->is_new = FALSE;

      return FALSE;
    }

    $relation->endpoints[LANGUAGE_NONE] = array(
      array(
        'entity_type' => $this->getLeftEntityType(),
        'entity_id'   => $left_entity_id,
      ),
      array(
        'entity_type' => $this->getRightEntityType(),
        'entity_id'   => $right_entity_id,
      ),
    );
  }

  /**
   * Entity type for first endpoint entity.
   */
  public function getLeftEntityType() {
    return 'user';
  }

  /**
   * Entity type for second endpoint entity.
   */
  public function getRightEntityType() {
    return 'node';
  }

}

/**
 * Class DwRelationUserAndPersonImport.
 *
 * Base class to import relations between Users and Persons.
 */
class DwRelationUserAndPersonImport extends DwRelationBaseImport {
  /**
   * {@inheritdoc}
   */
  public function getInstanceName() {
    return 'DwRelationUserAndPerson';
  }

  /**
   * {@inheritdoc}
   */
  public function getSourcePk() {
    return array(
      'person_id' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => TRUE,
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = $this->sourceConnection
      ->select('cont_person', 'cp')
      ->orderBy('cp.person_id', 'ASC');

    $query->addField('cp', 'person_id');
    $query->addField('cp', 'person_id', 'left_entity_id');
    $query->addField('cp', 'person_id', 'right_entity_id');

    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function __construct($arguments = array()) {
    parent::__construct($arguments);

    $this->addFieldMapping('left_entity_id', 'left_entity_id')
      ->sourceMigration(
        'DwUserRegular',
        'DwUserAdvanced',
        'DwUserModerator'
      );

    $this->addFieldMapping('right_entity_id', 'right_entity_id')
      ->sourceMigration('DwVideoFromPerson');
  }

}

chriscalip’s picture

@vlad.dancer
I also have to do a migration of d7 site , relation, to another d7 site, relation. I am currently seeing that relation_migrate does not have
relation to relation migration.. only references to relation.

I opened up issue:
relation_migrate migrate from relation to relation.
https://www.drupal.org/node/2551021

Currently prepping to do it at a custom drush script level because im thinking endpoints are different entity types & bundles.. which on my end i've done as different migrations..

chriscalip’s picture

Hello All,

Migration classes that facilitates migration of relation now available at relation module.
Also in the tradition of migrate_d2d, an example module relation_migrate_example is also provided.

Issue at:
relation_migrate migrate from relation to relation.
https://www.drupal.org/node/2551021