You are here

public function Table::import in Migrate Plus 8.5

Same name and namespace in other branches
  1. 8.3 src/Plugin/migrate/destination/Table.php \Drupal\migrate_plus\Plugin\migrate\destination\Table::import()
  2. 8.4 src/Plugin/migrate/destination/Table.php \Drupal\migrate_plus\Plugin\migrate\destination\Table::import()

Import the row.

Derived classes must implement import(), to construct one new object (pre-populated) using ID mappings in the Migration.

Parameters

\Drupal\migrate\Row $row: The row object.

array $old_destination_id_values: (optional) The old destination IDs. Defaults to an empty array.

Return value

array|bool An indexed array of destination IDs in the same order as defined in the plugin's getIds() method if the plugin wants to save the IDs to the ID map, TRUE to indicate success without saving IDs to the ID map, or FALSE to indicate a failure.

Overrides MigrateDestinationInterface::import

File

src/Plugin/migrate/destination/Table.php, line 182

Class

Table
Provides table destination plugin.

Namespace

Drupal\migrate_plus\Plugin\migrate\destination

Code

public function import(Row $row, array $old_destination_id_values = []) {

  // Skip batching (if configured) for updates.
  $batch_inserts = $this->batchSize > 1 && empty($old_destination_id_values);
  $ids = [];
  foreach ($this->idFields as $field => $fieldInfo) {
    if ($row
      ->hasDestinationProperty($field)) {
      $ids[$field] = $row
        ->getDestinationProperty($field);
    }
    elseif (!$row
      ->hasDestinationProperty($field) && empty($fieldInfo['use_auto_increment'])) {
      throw new MigrateSkipProcessException('All the id fields are required for a table migration.');
    }
    elseif ($batch_inserts && $fieldInfo['use_auto_increment']) {
      if (count($this->rowsToInsert) === 0) {

        // Get the highest existing ID, so we will create IDs above it.
        $this->lastId = $this->dbConnection
          ->query("SELECT MAX({$field}) AS MaxId FROM {{$this->tableName}}")
          ->fetchField();
        if (!$this->lastId) {
          $this->lastId = 0;
        }
      }
      $id = ++$this->lastId;
      $ids[$field] = $id;
      $row
        ->setDestinationProperty($field, $id);
    }
  }

  // When batching, make sure we have the same properties in the same order
  // every time.
  $values = [];
  if ($batch_inserts) {
    $destination_properties = array_keys($this->migration
      ->getProcess());
    $destination_properties = array_merge($destination_properties, array_keys($this->idFields));
    sort($destination_properties);
    $destination_values = $row
      ->getDestination();
    foreach ($destination_properties as $property_name) {
      $values[$property_name] = $destination_values[$property_name] ?? NULL;
    }
  }
  else {
    $values = $row
      ->getDestination();
  }
  if ($this->fields) {
    $values = array_intersect_key($values, $this->fields);
  }
  if ($batch_inserts) {
    $this->rowsToInsert[] = $values;
    if (count($this->rowsToInsert) >= $this->batchSize) {
      $this
        ->flushInserts();
    }
    $status = TRUE;
  }
  elseif (count($ids) < count($this->idFields)) {
    $status = $id = $this->dbConnection
      ->insert($this->tableName)
      ->fields($values)
      ->execute();
    foreach ($this->idFields as $field => $fieldInfo) {
      if (isset($fieldInfo['use_auto_increment']) && $fieldInfo['use_auto_increment'] === TRUE && !$row
        ->hasDestinationProperty($field)) {
        $row
          ->setDestinationProperty($field, $id);
        $ids[$field] = $id;
      }
    }
  }
  else {
    $status = $this->dbConnection
      ->merge($this->tableName)
      ->keys($ids)
      ->fields($values)
      ->execute();
  }
  return $status ? $ids : NULL;
}