You are here

public function MigrateDestinationTable::import in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/destinations/table.inc \MigrateDestinationTable::import()

Import a single row.

Parameters

$entity: Object object to build. Prefilled with any fields mapped in the Migration.

$row: Raw source data object - passed through to prepare/complete handlers.

Return value

array Array of key fields of the object that was saved if successful. FALSE on failure.

Overrides MigrateDestination::import

1 method overrides MigrateDestinationTable::import()
MigrateDestinationRole::import in plugins/destinations/user.inc
Import a single row.

File

plugins/destinations/table.inc, line 90
Support for tables defined through the Schema API.

Class

MigrateDestinationTable
Destination class implementing migration into a single table defined through the Schema API.

Code

public function import(stdClass $entity, stdClass $row) {
  if (empty($this->schema['primary key'])) {
    throw new MigrateException(t("The destination table has no primary key defined."));
  }

  // Only filled when doing an update.
  $primary_key = array();
  $migration = Migration::currentMigration();

  // Updating previously-migrated content?
  if (isset($row->migrate_map_destid1)) {
    $i = 1;
    foreach ($this->schema['primary key'] as $key) {
      $primary_key[] = $key;
      $destination_id = $row->{'migrate_map_destid' . $i};
      if (isset($entity->{$key})) {
        if ($entity->{$key} != $destination_id) {
          throw new MigrateException(t("Incoming id !id and map destination id !destid don't match", array(
            '!id' => $entity->{$key},
            '!destid' => $destination_id,
          )));
        }
      }
      else {
        $entity->{$key} = $destination_id;
      }
      $i++;
    }
  }
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    foreach ($this->schema['primary key'] as $key) {
      $primary_key[] = $key;
      if (!isset($entity->{$key})) {
        throw new MigrateException(t('System-of-record is DESTINATION, but no destination id provided'));
      }
    }
    $select = db_select($this->tableName)
      ->fields($this->tableName);
    foreach ($this->schema['primary key'] as $key) {
      $select
        ->condition($key, $entity->{$key});
    }
    $old_entity = $select
      ->execute()
      ->fetchObject();
    if (empty($old_entity)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but the destination entity does not exist'));
    }
    foreach ($entity as $field => $value) {
      $old_entity->{$field} = $entity->{$field};
    }
    $entity = $old_entity;
  }
  $this
    ->prepare($entity, $row);
  $status = drupal_write_record($this->tableName, $entity, $primary_key);
  $this
    ->complete($entity, $row);
  if ($status) {
    $id = array();
    foreach ($this->schema['primary key'] as $key) {
      $id[] = $entity->{$key};
    }

    // Increment the number of updated or inserted records by checking the
    // result of drupal_write_record.
    $status == SAVED_NEW ? $this->numCreated++ : $this->numUpdated++;
    return $id;
  }
}