You are here

class MigrateDestinationRelation in Relation 7

Same name and namespace in other branches
  1. 8.2 relation_migrate/relation_migrate.destination.inc \MigrateDestinationRelation
  2. 8 relation_migrate/relation_migrate.destination.inc \MigrateDestinationRelation

Destination class implementing migration into relation.

Hierarchy

Expanded class hierarchy of MigrateDestinationRelation

File

relation_migrate/relation_migrate.destination.inc, line 11
Support for relation destinations.

View source
class MigrateDestinationRelation extends MigrateDestinationEntity {
  public static function getKeySchema() {
    return array(
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Unique relation id (entity id).',
      ),
    );
  }

  /**
   * Basic initialization
   *
   * @param string $bundle
   *  A.k.a. the content type (page, article, etc.) of the node.
   * @param array $options
   *  Options applied to nodes.
   */
  public function __construct($bundle, array $options = array()) {
    parent::__construct('relation', $bundle, $options);
  }

  /**
   * Returns a list of fields available to be mapped for the relation type (bundle)
   *
   * @return array
   *  Keys: machine names of the fields (to be passed to addFieldMapping)
   *  Values: Human-friendly descriptions of the fields.
   */
  public function fields() {
    $fields = array();

    // First the core (relation table) properties
    $fields['rid'] = t('Relation: Existing relation ID');
    $fields['is_new'] = t('Relation: Indicates a new relation with the specified rid should be created');
    $fields['uid'] = t('Relation: Authored by (uid)');
    $fields['created'] = t('Relation: Created timestamp');
    $fields['changed'] = t('Relation: Modified timestamp');

    // Then add in anything provided by handlers
    $fields += migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle);
    $fields += migrate_handler_invoke_all('Relation', 'fields', $this->entityType, $this->bundle);
    return $fields;
  }

  /**
   * Delete a batch of relations at once.
   *
   * @param $rids
   *  Array of relation IDs to be deleted.
   */
  public function bulkRollback(array $rids) {
    migrate_instrument_start('relation_delete_multiple');
    $this
      ->prepareRollback($rids);
    relation_delete_multiple($rids);
    $this
      ->completeRollback($rids);
    migrate_instrument_stop('relation_delete_multiple');
  }

  /**
   * Import a single relation.
   *
   * @param $relation
   *  Relation object to build. Prefilled with any fields mapped in the Migration.
   * @param $row
   *  Raw source data object - passed through to prepare/complete handlers.
   *
   * @return array
   *  Array of key fields (rid only in this case) of the relation that was saved if
   *  successful. FALSE on failure.
   */
  public function import(stdClass $relation, stdClass $row) {

    // Updating previously-migrated content?
    $migration = Migration::currentMigration();
    if (isset($row->migrate_map_destid1)) {

      // Make sure is_new is off
      $relation->is_new = FALSE;
      if (isset($relation->rid)) {
        if ($relation->rid != $row->migrate_map_destid1) {
          throw new MigrateException(t("Incoming rid !rid and map destination rid !destid1 don't match", array(
            '!rid' => $relation->rid,
            '!destid1' => $row->migrate_map_destid1,
          )));
        }
      }
      else {
        $relation->rid = $row->migrate_map_destid1;
      }

      // Get the existing vid, so updates don't generate notices
      $values = db_select('relation', 'r')
        ->fields('r', array(
        'vid',
      ))
        ->condition('rid', $relation->rid)
        ->execute()
        ->fetchAssoc();
      $relation->vid = $values['vid'];
    }
    if ($migration
      ->getSystemOfRecord() == Migration::DESTINATION) {
      if (!isset($relation->rid)) {
        throw new MigrateException(t('System-of-record is DESTINATION, but no destination rid provided'));
      }
      $old_relation = relation_load($relation->rid);
      if (!isset($relation->created)) {
        $relation->created = $old_relation->created;
      }
      if (!isset($relation->vid)) {
        $relation->vid = $old_relation->vid;
      }
      if (!isset($relation->uid)) {
        $relation->uid = $old_relation->uid;
      }
    }

    // Set some required properties.
    if (!isset($relation->uid)) {
      $relation->uid = $GLOBALS['user']->uid;
    }

    // Set type before invoking prepare handlers - they may take type-dependent actions.
    $relation->relation_type = $this->bundle;
    if ($migration
      ->getSystemOfRecord() == Migration::SOURCE) {

      // relation_save() will blow these away, so save them here and
      // save them later.
      if (isset($relation->created)) {
        $created = MigrationBase::timestamp($relation->created);
      }
      if (isset($relation->changed)) {
        $changed = MigrationBase::timestamp($relation->changed);
      }
    }

    // Trying to update an existing relation
    if ($migration
      ->getSystemOfRecord() == Migration::DESTINATION) {

      // Incoming data overrides existing data, so only copy non-existent fields
      foreach ($old_node as $field => $value) {

        // An explicit NULL in the source data means to wipe to old value (i.e.,
        // don't copy it over from $old_node)
        if (property_exists($relation, $field) && $relation->{$field} === NULL) {

          // Ignore this field
        }
        elseif (!isset($relation->{$field})) {
          $relation->{$field} = $old_relation->{$field};
        }
      }
    }

    // Invoke migration prepare handlers
    $this
      ->prepare($relation, $row);
    if (isset($relation->rid) && empty($relation->is_new)) {
      $updating = TRUE;
    }
    else {
      $updating = FALSE;
    }

    // Save relation object
    migrate_instrument_start('relation_save');
    $rid = relation_save($relation);
    migrate_instrument_stop('relation_save');
    if (isset($relation->rid)) {
      if ($updating) {
        $this->numUpdated++;
      }
      else {
        $this->numCreated++;
      }

      // Update changed and created dates if needed.
      if (isset($changed)) {
        db_update('relation')
          ->fields(array(
          'changed' => $changed,
        ))
          ->condition('rid', $relation->rid)
          ->execute();
        $relation->changed = $changed;
      }
      if (isset($created)) {
        db_update('relation')
          ->fields(array(
          'created' => $created,
        ))
          ->condition('rid', $relation->rid)
          ->execute();
        $relation->created = $created;
      }
      $return = array(
        $relation->rid,
      );
    }
    else {
      $return = FALSE;
    }
    $this
      ->complete($relation, $row);
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateDestination::$numCreated protected property Maintain stats on the number of destination objects created or updated.
MigrateDestination::$numUpdated protected property
MigrateDestination::getCreated public function
MigrateDestination::getUpdated public function
MigrateDestination::resetStats public function Reset numCreated and numUpdated back to 0.
MigrateDestinationEntity::$bundle protected property The bundle (node type, vocabulary, etc.) of the destination.
MigrateDestinationEntity::$entityType protected property The entity type (node, user, taxonomy_term, etc.) of the destination.
MigrateDestinationEntity::$language protected property Default language for text fields in this destination.
MigrateDestinationEntity::$textFormat protected property Default input format for text fields in this destination.
MigrateDestinationEntity::array_flatten public static function Flattens an array of allowed values.
MigrateDestinationEntity::complete public function Give handlers a shot at modifying the object (or taking additional action) after saving it.
MigrateDestinationEntity::completeRollback public function Give handlers a shot at cleaning up after an entity has been rolled back.
MigrateDestinationEntity::fieldAttachValidate public static function Perform field validation against the field data in an entity. Wraps field_attach_validate to handle exceptions cleanly and provide maximum information for identifying the cause of validation errors.
MigrateDestinationEntity::getBundle public function
MigrateDestinationEntity::getEntityType public function
MigrateDestinationEntity::getLanguage public function
MigrateDestinationEntity::getTextFormat public function
MigrateDestinationEntity::prepare public function Give handlers a shot at modifying the object before saving it.
MigrateDestinationEntity::prepareRollback public function Give handlers a shot at cleaning up before an entity has been rolled back.
MigrateDestinationEntity::__toString public function Derived classes must implement __toString(). Overrides MigrateDestination::__toString
MigrateDestinationRelation::bulkRollback public function Delete a batch of relations at once.
MigrateDestinationRelation::fields public function Returns a list of fields available to be mapped for the relation type (bundle) Overrides MigrateDestination::fields
MigrateDestinationRelation::getKeySchema public static function
MigrateDestinationRelation::import public function Import a single relation. Overrides MigrateDestination::import
MigrateDestinationRelation::__construct public function Basic initialization Overrides MigrateDestinationEntity::__construct