You are here

public function CRMCoreMigrateSQLMap::saveIDMapping in CRM Core 7

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

Parameters

stdClass $source_row: The raw source data. We use the key map derived from the source object to get the source key values.

array $dest_ids: The destination key values.

int $needs_update: Status of the source row in the map. Defaults to STATUS_IMPORTED.

int $rollback_action: How to handle the destination object on rollback. Defaults to ROLLBACK_DELETE. $param string $hash If hashing is enabled, the hash of the raw source row.

Overrides MigrateSQLMap::saveIDMapping

File

modules/crm_core_data_import/includes/controllers/CRMCoreMigrateSQLMap.inc, line 179
Defines a CRMCore extention of MigrateSQLMap

Class

CRMCoreMigrateSQLMap
@file Defines a CRMCore extention of MigrateSQLMap

Code

public function saveIDMapping(stdClass $source_row, array $dest_ids, $needs_update = MigrateMap::STATUS_IMPORTED, $rollback_action = MigrateMap::ROLLBACK_DELETE, $hash = NULL) {
  migrate_instrument_start('saveIDMapping');

  // Construct the source key
  $keys = array();
  foreach ($this->sourceKeyMap as $field_name => $key_name) {

    // A NULL key value will fail.
    if (is_null($source_row->{$field_name})) {
      Migration::displayMessage(t('Could not save to map table due to NULL value for key field !field', array(
        '!field' => $field_name,
      )));
      migrate_instrument_stop('saveIDMapping');
      return;
    }
    $keys[$key_name] = $source_row->{$field_name};
  }
  $fields = array(
    'needs_update' => (int) $needs_update,
    'rollback_action' => (int) $rollback_action,
    'hash' => $hash,
  );

  // Add relation ids when present.
  foreach ($this->importer
    ->getRelationDestinationEndPoints(implode(':', array(
    $this->options['entity_type'],
    $this->options['entity_bundle'],
    $this->options['delta'],
  ))) as $key => $ep) {
    list($ep_entity_type, $ep_bundle, $ep_importer_id) = explode(':', $ep);
    $endpoint_machine_name = _crm_core_data_import_migration_machine_name($ep_importer_id, $ep_entity_type, $ep_bundle, $this->options['delta']);
    $endpoint_migration = Migration::getInstance($endpoint_machine_name);
    $ep_source_map = array_keys($endpoint_migration
      ->getMap()
      ->getSourceKeyMap());
    $fields['relation_id' . ($key + 1)] = $source_row->{$ep_source_map[0]};
  }
  $count = 1;
  if (!empty($dest_ids)) {
    foreach ($dest_ids as $dest_id) {
      $fields['destid' . $count++] = $dest_id;
    }
  }
  if ($this->trackLastImported) {
    $fields['last_imported'] = time();
  }
  $this->connection
    ->merge($this->mapTable)
    ->key($keys)
    ->fields($fields)
    ->execute();
  migrate_instrument_stop('saveIDMapping');
}