Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Database.php

function Database::getConnection

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

Parameters

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.

23 calls to Database::getConnection()
BootstrapConfigStorageFactory::getDatabaseStorage in core/lib/Drupal/Core/Config/BootstrapConfigStorageFactory.php
Returns a Database configuration storage implementation.
DatabaseDriverUninstallValidator::validate in core/lib/Drupal/Core/Extension/DatabaseDriverUninstallValidator.php
Determines the reasons a module can not be uninstalled.
DbCommandBase::getDatabaseConnection in core/lib/Drupal/Core/Command/DbCommandBase.php
Parse input options decide on a database.
DbLog::log in core/modules/dblog/src/Logger/DbLog.php
Logs with an arbitrary level.
drupal_install_system in core/includes/install.inc
Installs the system module.

... See full list

File

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

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
    // "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];
}
RSS feed
Powered by Drupal