saveIdMapping example


  protected function prepareMigrations(array $id_mappings) {
    $manager = $this->container->get('plugin.manager.migration');
    foreach ($id_mappings as $migration_id => $data) {
      foreach ($manager->createInstances($migration_id) as $migration) {
        $id_map = $migration->getIdMap();
        $id_map->setMessage($this);
        $source_ids = $migration->getSourcePlugin()->getIds();
        foreach ($data as $id_mapping) {
          $row = new Row(array_combine(array_keys($source_ids)$id_mapping[0])$source_ids);
          $id_map->saveIdMapping($row$id_mapping[1]);
        }
      }
    }
  }

  /** * Modify a migration's configuration before executing it. * * @param \Drupal\migrate\Plugin\MigrationInterface $migration * The migration to execute. */
  

  public function testSaveIdMapping() {
    $source = [
      'source_id_property' => 'source_value',
    ];
    $row = new Row($source['source_id_property' => []]);
    $id_map = $this->getIdMap();
    $id_map->saveIdMapping($row['destination_id_property' => 2]);
    $expected_result = [
      [
        'sourceid1' => 'source_value',
        'source_ids_hash' => $this->getIdMap()->getSourceIdsHash($source),
        'destid1' => 2,
      ] + $this->idMapDefaults(),
    ];
    $this->queryResultTest($this->getIdMapContents()$expected_result);
    $source = [
      'source_id_property' => 'source_value_1',
    ];
    
foreach ($default_values as $key => $value) {
      $row->setDestinationProperty($key$value);
    }
    $destination_ids = [];
    try {
      $destination_ids = $destination->import($row);
    }
    catch (\Exception $e) {
      $id_map->saveMessage($row->getSourceIdValues()$e->getMessage());
    }
    if ($destination_ids) {
      $id_map->saveIdMapping($row$destination_ids, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
      return $destination_ids;
    }
    return FALSE;
  }

}
$save_to_map = $e->getSaveToMap();
      if ($message = trim($e->getMessage())) {
        $this->idMap->saveMessage($row->getSourceIdValues()$message, MigrationInterface::MESSAGE_INFORMATIONAL);
      }
    }

    // We're explicitly skipping this row - keep track in the map table.     if ($skip) {
      // Make sure we replace any previous messages for this item with any       // new ones.       if ($save_to_map) {
        $this->idMap->saveIdMapping($row[], MigrateIdMapInterface::STATUS_IGNORED);
        $this->currentRow = NULL;
        $this->currentSourceIds = NULL;
      }
      $result = FALSE;
    }
    elseif ($this->trackChanges) {
      // When tracking changed data, We want to quietly skip (rather than       // "ignore") rows with changes. The caller needs to make that decision,       // so we need to provide them with the necessary information (before and       // after hashes).       $row->rehash();
    }


    // Get the process pipeline.     $pipeline = FALSE;
    if ($source->valid()) {
      try {
        $pipeline = $this->migration->getProcessPlugins();
      }
      catch (MigrateException $e) {
        $row = $source->current();
        $this->sourceIdValues = $row->getSourceIdValues();
        $this->getIdMap()->saveIdMapping($row[]$e->getStatus());
        $this->saveMessage($e->getMessage()$e->getLevel());
      }
    }

    $return = MigrationInterface::RESULT_COMPLETED;
    if ($pipeline) {
      $id_map = $this->getIdMap();
      $destination = $this->migration->getDestinationPlugin();
      while ($source->valid()) {
        $row = $source->current();
        $this->sourceIdValues = $row->getSourceIdValues();

        
    $preserved_term_ids[] = 1;
    $new_term = Term::create(['tid' => 1, 'vid' => 1, 'name' => 'music']);
    $new_term->save();

    // Import and validate term entities were created.     $term_executable = new MigrateExecutable($term_migration$this);
    $term_executable->import();
    // Also explicitly mark one row to be preserved on rollback.     $preserved_term_ids[] = 2;
    $map_row = $term_id_map->getRowBySource(['id' => 2]);
    $dummy_row = new Row(['id' => 2]$ids);
    $term_id_map->saveIdMapping($dummy_row[$map_row['destid1']],
      $map_row['source_row_status'], MigrateIdMapInterface::ROLLBACK_PRESERVE);

    foreach ($term_data_rows as $row) {
      /** @var \Drupal\taxonomy\Entity\Term $term */
      $term = Term::load($row['id']);
      $this->assertNotEmpty($term);
      $map_row = $term_id_map->getRowBySource(['id' => $row['id']]);
      $this->assertNotNull($map_row['destid1']);
    }

    // Add a failed row to test if this can be rolled back without errors.
Home | Imprint | This part of the site doesn't use cookies.