function Sql::saveIdMapping

Same name in other branches
  1. 9 core/modules/migrate/src/Plugin/migrate/id_map/Sql.php \Drupal\migrate\Plugin\migrate\id_map\Sql::saveIdMapping()
  2. 10 core/modules/migrate/src/Plugin/migrate/id_map/Sql.php \Drupal\migrate\Plugin\migrate\id_map\Sql::saveIdMapping()
  3. 11.x core/modules/migrate/src/Plugin/migrate/id_map/Sql.php \Drupal\migrate\Plugin\migrate\id_map\Sql::saveIdMapping()

Overrides MigrateIdMapInterface::saveIdMapping

File

core/modules/migrate/src/Plugin/migrate/id_map/Sql.php, line 619

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.
    $fields = [];
    foreach ($this->sourceIdFields() as $field_name => $key_name) {
        // A NULL key value is usually an indication of a problem.
        if (!isset($source_id_values[$field_name])) {
            $this->message
                ->display($this->t('Did not save to map table due to NULL value for key field @field', [
                '@field' => $field_name,
            ]), 'error');
            return;
        }
        $fields[$key_name] = $source_id_values[$field_name];
    }
    if (!$fields) {
        return;
    }
    $fields += [
        '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
        ->getTrackLastImported()) {
        $fields['last_imported'] = time();
    }
    $keys = [
        $this::SOURCE_IDS_HASH => $this->getSourceIdsHash($source_id_values),
    ];
    // Notify anyone listening of the map row we're about to save.
    $this->eventDispatcher
        ->dispatch(MigrateEvents::MAP_SAVE, new MigrateMapSaveEvent($this, $fields));
    $this->getDatabase()
        ->merge($this->mapTableName())
        ->key($keys)
        ->fields($fields)
        ->execute();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.