function Database::openConnection

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::openConnection()
  2. 10 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::openConnection()
  3. 9 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::openConnection()
  4. 8.9.x core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::openConnection()
  5. 7.x includes/database/database.inc \Database::openConnection()

Opens a connection to the server specified by the given key and target.

Parameters

string $key: The database connection key, as specified in settings.php.

string $target: The database target to open.

Return value

\Drupal\Core\Database\Connection The opened database connection.

Throws

\Drupal\Core\Database\ConnectionNotDefinedException

\Drupal\Core\Database\DriverNotSpecifiedException

1 call to Database::openConnection()
Database::getConnection in core/lib/Drupal/Core/Database/Database.php
Gets the connection object for the specified database key and target.

File

core/lib/Drupal/Core/Database/Database.php, line 495

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

final protected static function openConnection(string|int $key, string|int $target) : Connection {
  if (is_int($key)) {
    @trigger_error('Passing an integer value to the $key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
  }
  if (is_int($target)) {
    @trigger_error('Passing an integer value to the $target parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
  }
  // If the requested database does not exist then it is an unrecoverable
  // error.
  if (!isset(self::$databaseInfo[$key])) {
    throw new ConnectionNotDefinedException('The specified database connection is not defined: ' . $key);
  }
  if (!self::$databaseInfo[$key][$target]['driver']) {
    throw new DriverNotSpecifiedException('Driver not specified for this database connection: ' . $key);
  }
  $driver_class = self::$databaseInfo[$key][$target]['namespace'] . '\\Connection';
  $client_connection = $driver_class::open(self::$databaseInfo[$key][$target]);
  $new_connection = new $driver_class($client_connection, self::$databaseInfo[$key][$target]);
  assert($new_connection instanceof Connection);
  $new_connection->setTarget((string) $target);
  $new_connection->setKey((string) $key);
  // If we have any active logging objects for this connection key, we need
  // to associate them with the connection we just opened.
  if (!empty(self::$logs[$key])) {
    $new_connection->enableEvents(StatementEvent::all());
    $new_connection->setLogger(self::$logs[$key]);
  }
  return $new_connection;
}

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