You are here

public function Sql::lookupDestinationIds in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/migrate/src/Plugin/migrate/id_map/Sql.php \Drupal\migrate\Plugin\migrate\id_map\Sql::lookupDestinationIds()
  2. 9 core/modules/migrate/src/Plugin/migrate/id_map/Sql.php \Drupal\migrate\Plugin\migrate\id_map\Sql::lookupDestinationIds()

Looks up the destination identifiers corresponding to a source key.

This can look up a subset of source keys if only some are provided, and will return all destination keys that match.

Parameters

array $source_id_values: The source identifier keyed values of the records, e.g. ['nid' => 5]. If unkeyed, the first count($source_id_values) keys will be assumed.

Return value

array An array of arrays of destination identifier values.

Throws

\Drupal\migrate\MigrateException Thrown when $source_id_values contains unknown keys, or is the wrong length.

Overrides MigrateIdMapInterface::lookupDestinationIds

File

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

Class

Sql
Defines the sql based ID map implementation.

Namespace

Drupal\migrate\Plugin\migrate\id_map

Code

public function lookupDestinationIds(array $source_id_values) {
  if (empty($source_id_values)) {
    return [];
  }

  // Canonicalize the keys into a hash of DB-field => value.
  $is_associative = !isset($source_id_values[0]);
  $conditions = [];
  foreach ($this
    ->sourceIdFields() as $field_name => $db_field) {
    if ($is_associative) {

      // Ensure to handle array elements with a NULL value.
      if (array_key_exists($field_name, $source_id_values)) {

        // Associative $source_id_values can have fields out of order.
        if (isset($source_id_values[$field_name])) {

          // Only add a condition if the value is not NULL.
          $conditions[$db_field] = $source_id_values[$field_name];
        }
        unset($source_id_values[$field_name]);
      }
    }
    else {

      // For non-associative $source_id_values, we assume they're the first
      // few fields.
      if (empty($source_id_values)) {
        break;
      }
      $conditions[$db_field] = array_shift($source_id_values);
    }
  }
  if (!empty($source_id_values)) {
    $var_dump = var_export($source_id_values, TRUE);
    throw new MigrateException(sprintf("Extra unknown items for map %s in source IDs: %s", $this
      ->mapTableName(), $var_dump));
  }
  $query = $this
    ->getDatabase()
    ->select($this
    ->mapTableName(), 'map')
    ->fields('map', $this
    ->destinationIdFields());
  if (count($this
    ->sourceIdFields()) === count($conditions)) {

    // Optimization: Use the primary key.
    $query
      ->condition($this::SOURCE_IDS_HASH, $this
      ->getSourceIdsHash(array_values($conditions)));
  }
  else {
    foreach ($conditions as $db_field => $value) {
      $query
        ->condition($db_field, $value);
    }
  }
  try {
    return $query
      ->execute()
      ->fetchAll(\PDO::FETCH_NUM);
  } catch (DatabaseExceptionWrapper $e) {

    // It's possible that the query will cause an exception to be thrown. For
    // example, the URL alias migration uses a dummy node ID of 'INVALID_NID'
    // to cause the lookup to return no results. On PostgreSQL this causes an
    // exception because 'INVALID_NID' is not the expected type.
    return [];
  }
}