function SqlBase::setUpDatabase

Same name and namespace in other branches
  1. 9 core/modules/migrate/src/Plugin/migrate/source/SqlBase.php \Drupal\migrate\Plugin\migrate\source\SqlBase::setUpDatabase()
  2. 8.9.x core/modules/migrate/src/Plugin/migrate/source/SqlBase.php \Drupal\migrate\Plugin\migrate\source\SqlBase::setUpDatabase()
  3. 10 core/modules/migrate/src/Plugin/migrate/source/SqlBase.php \Drupal\migrate\Plugin\migrate\source\SqlBase::setUpDatabase()

Gets a connection to the referenced database.

This method will add the database connection if necessary.

Parameters

array $database_info: Configuration for the source database connection. The keys are: 'key' - The database connection key. 'target' - The database connection target. 'database' - Database configuration array as accepted by Database::addConnectionInfo.

Return value

\Drupal\Core\Database\Connection The connection to use for this plugin's queries.

Throws

\Drupal\migrate\Exception\RequirementsException Thrown if no source database connection is configured.

1 call to SqlBase::setUpDatabase()
SqlBase::getDatabase in core/modules/migrate/src/Plugin/migrate/source/SqlBase.php
Gets the database connection object.

File

core/modules/migrate/src/Plugin/migrate/source/SqlBase.php, line 185

Class

SqlBase
Sources whose data may be fetched via a database connection.

Namespace

Drupal\migrate\Plugin\migrate\source

Code

protected function setUpDatabase(array $database_info) {
    // If there is no explicit database configuration at all, fall back to a
    // connection named 'migrate'.
    $key = $database_info['key'] ?? 'migrate';
    $target = $database_info['target'] ?? 'default';
    if (isset($database_info['database'])) {
        Database::addConnectionInfo($key, $target, $database_info['database']);
    }
    try {
        $connection = Database::getConnection($target, $key);
    } catch (ConnectionNotDefinedException $e) {
        // If we fell back to the magic 'migrate' connection and it doesn't exist,
        // treat the lack of the connection as a RequirementsException.
        if ($key == 'migrate') {
            throw new RequirementsException("No database connection configured for source plugin " . $this->pluginId, [], 0, $e);
        }
        else {
            throw $e;
        }
    }
    return $connection;
}

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