function Sql::lookupDestinationIds

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

Overrides MigrateIdMapInterface::lookupDestinationIds

1 call to Sql::lookupDestinationIds()
Sql::lookupDestinationId in core/modules/migrate/src/Plugin/migrate/id_map/Sql.php
Looks up the destination identifier corresponding to a source key.

File

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

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 [];
    }
}

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