You are here

public function MigrateDestinationTableCopy::import in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/destinations/table_copy.inc \MigrateDestinationTableCopy::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

File

plugins/destinations/table_copy.inc, line 50
Copies data_row into a table using drupal_write_record()

Class

MigrateDestinationTableCopy
Destination class implementing migration into a single table.

Code

public function import(stdClass $entity, stdClass $row) {
  $migration = MigrationBase::currentMigration();
  $fields = clone $row;

  // Remove all map data, otherwise we'll try to write it to the destination
  // table.
  foreach ($fields as $field => $data) {
    if (strpos($field, 'migrate_map_') === 0) {
      unset($fields->{$field});
    }
  }
  $keys = array_keys($this->keySchema);
  $values = array();
  foreach ($keys as $key) {
    $values[] = $row->{$key};
  }
  $query = db_merge($this->tableName)
    ->key($keys, $values)
    ->fields((array) $fields);
  try {
    $status = $query
      ->execute();
    if ($status == MergeQuery::STATUS_INSERT) {
      $this->numCreated++;
    }
    else {
      $this->numUpdated++;
    }
    return $values;
  } catch (MigrateException $e) {
    $migration
      ->saveMessage($e
      ->getMessage(), $e
      ->getLevel());
    Migration::displayMessage($e
      ->getMessage());
  } catch (Exception $e) {
    $migration
      ->handleException($e);
  }
}