8.5.x Database.php Database::getConnection($target = 'default', $key = NULL)
8.0.x Database.php Database::getConnection($target = 'default', $key = NULL)
8.1.x Database.php Database::getConnection($target = 'default', $key = NULL)
8.2.x Database.php Database::getConnection($target = 'default', $key = NULL)
8.3.x Database.php Database::getConnection($target = 'default', $key = NULL)
8.4.x Database.php Database::getConnection($target = 'default', $key = NULL)
8.6.x Database.php Database::getConnection($target = 'default', $key = NULL)
7.x database.inc Database::getConnection($target = 'default', $key = NULL)

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


string $target: The database target name.

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

Return value

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

102 calls to Database::getConnection()
AliasTest::testCRUD in core/modules/system/src/Tests/Path/AliasTest.php
AliasTest::testLookupPath in core/modules/system/src/Tests/Path/AliasTest.php
AliasTest::testWhitelist in core/modules/system/src/Tests/Path/AliasTest.php
Tests the alias whitelist.
argument-placeholder.php in core/modules/views/tests/fixtures/update/argument-placeholder.php
Text fixture.
ConnectionFailureTest::testConnectionFailureLogging in core/modules/dblog/src/Tests/ConnectionFailureTest.php
Tests logging of connection failures.

... See full list

1 string reference to 'Database::getConnection'
core.services.yml in core/core.services.yml


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


Primary front-controller for the database system.




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
  // "replica", indicating to use a replica SQL server if one is available. If
  // it's not available, then the default/primary 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];