final public static function Database::getConnection

Gets the connection object for the specified database key and target.

Parameters

$target: The database target name.

$key: The database connection key. Defaults to NULL which means the active key.

Return value

Drupal\Core\Database\Connection The corresponding connection object.

40 calls to Database::getConnection()
AliasTest::tearDown in drupal/core/modules/system/lib/Drupal/system/Tests/Path/AliasTest.php
Deletes created files, database tables, and reverts all environment changes.
AliasTest::testCRUD in drupal/core/modules/system/lib/Drupal/system/Tests/Path/AliasTest.php
AliasTest::testLookupPath in drupal/core/modules/system/lib/Drupal/system/Tests/Path/AliasTest.php
ConnectionTest::testConnectionClosing in drupal/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionTest.php
Tests the closing of a database connection.
ConnectionTest::testConnectionOptions in drupal/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionTest.php
Tests the connection options of the active database.

... See full list

File

drupal/core/lib/Drupal/Core/Database/Database.php, line 155
Definition of Drupal\Core\Database\Database

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

public static final function getConnection($target = 'default', $key = NULL) {
  if (!isset($key)) {

    // By default, we want the active connection, set in setActiveConnection.
    $key = self::$activeKey;
  }

  // If the requested target does not exist, or if it is ignored, we fall back
  // to the default target. The target is typically either "default" or
  // "slave", indicating to use a slave SQL server if one is available. If
  // it's not available, then the default/master server is the correct server
  // to use.
  if (!empty(self::$ignoreTargets[$key][$target]) || !isset(self::$databaseInfo[$key][$target])) {
    $target = 'default';
  }
  if (!isset(self::$connections[$key][$target])) {

    // If necessary, a new connection is opened.
    self::$connections[$key][$target] = self::openConnection($key, $target);
  }
  return self::$connections[$key][$target];
}