You are here

public function Sql::saveIdMapping in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/migrate/src/Plugin/migrate/id_map/Sql.php \Drupal\migrate\Plugin\migrate\id_map\Sql::saveIdMapping()

Saves a mapping from the source identifiers to the destination identifiers.

Called upon import of one row, we record a mapping from the source ID to the destination ID. Also may be called, setting the third parameter to NEEDS_UPDATE, to signal an existing record should be re-migrated.

Parameters

\Drupal\migrate\Row $row: The raw source data. We use the ID map derived from the source object to get the source identifier values.

array $destination_id_values: An array of destination identifier values.

int $status: Status of the source row in the map.

int $rollback_action: How to handle the destination object on rollback.

Overrides MigrateIdMapInterface::saveIdMapping

File

core/modules/migrate/src/Plugin/migrate/id_map/Sql.php, line 509
Contains \Drupal\migrate\Plugin\migrate\id_map\Sql.

Class

Sql
Defines the sql based ID map implementation.

Namespace

Drupal\migrate\Plugin\migrate\id_map

Code

public function saveIdMapping(Row $row, array $destination_id_values, $source_row_status = MigrateIdMapInterface::STATUS_IMPORTED, $rollback_action = MigrateIdMapInterface::ROLLBACK_DELETE) {

  // Construct the source key.
  $source_id_values = $row
    ->getSourceIdValues();

  // Construct the source key and initialize to empty variable keys.
  $keys = array();
  foreach ($this
    ->sourceIdFields() as $field_name => $key_name) {

    // A NULL key value will fail.
    if (!isset($source_id_values[$field_name])) {
      $this->message
        ->display(t('Could not save to map table due to NULL value for key field @field', array(
        '@field' => $field_name,
      )), 'error');
      return;
    }
    $keys[$key_name] = $source_id_values[$field_name];
  }
  $fields = array(
    'source_row_status' => (int) $source_row_status,
    'rollback_action' => (int) $rollback_action,
    'hash' => $row
      ->getHash(),
  );
  $count = 0;
  foreach ($destination_id_values as $dest_id) {
    $fields['destid' . ++$count] = $dest_id;
  }
  if ($count && $count != count($this
    ->destinationIdFields())) {
    $this->message
      ->display(t('Could not save to map table due to missing destination id values'), 'error');
    return;
  }
  if ($this->migration
    ->get('trackLastImported')) {
    $fields['last_imported'] = time();
  }
  if ($keys) {

    // Notify anyone listening of the map row we're about to save.
    $this->eventDispatcher
      ->dispatch(MigrateEvents::MAP_SAVE, new MigrateMapSaveEvent($this, $keys + $fields));
    $this
      ->getDatabase()
      ->merge($this
      ->mapTableName())
      ->key($keys)
      ->fields($fields)
      ->execute();
  }
}