Database::openConnection

final protected static Database::openConnection($key, $target)

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. The default is "default".

string $target: The database target to open.

Throws

\Drupal\Core\Database\ConnectionNotDefinedException

\Drupal\Core\Database\DriverNotSpecifiedException

File

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

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
final protected static function openConnection($key, $target) {
  // 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 (!$driver = self::$databaseInfo[$key][$target]['driver']) {
    throw new DriverNotSpecifiedException('Driver not specified for this database connection: ' . $key);
  }
 
  if (!empty(self::$databaseInfo[$key][$target]['namespace'])) {
    $driver_class = self::$databaseInfo[$key][$target]['namespace'] . '\\Connection';
  }
  else {
    // Fallback for Drupal 7 settings.php.
    $driver_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
  }
 
  $pdo_connection = $driver_class::open(self::$databaseInfo[$key][$target]);
  $new_connection = new $driver_class($pdo_connection, self::$databaseInfo[$key][$target]);
  $new_connection->setTarget($target);
  $new_connection->setKey($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->setLogger(self::$logs[$key]);
  }
 
  return $new_connection;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.