function Connection::__construct

Same name in this branch
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::__construct()
  2. 9 core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::__construct()
  3. 9 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::__construct()
Same name in other branches
  1. 8.9.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysqlDeprecatedVersion/Connection.php \Drupal\driver_test\Driver\Database\DrivertestMysqlDeprecatedVersion\Connection::__construct()
  2. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::__construct()
  3. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Connection.php \Drupal\Core\Database\Driver\mysql\Connection::__construct()
  4. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php \Drupal\Core\Database\Driver\pgsql\Connection::__construct()
  5. 8.9.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::__construct()
  6. 10 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::__construct()
  7. 10 core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::__construct()
  8. 10 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::__construct()
  9. 10 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::__construct()
  10. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::__construct()
  11. 11.x core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::__construct()
  12. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::__construct()
  13. 11.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::__construct()

Constructs a Connection object.

Parameters

object $connection: An object of the client class representing a database connection.

array $connection_options: An array of options for the connection. May include the following:

  • prefix
  • namespace
  • Other driver-specific options.

An 'extra_prefix' option may be present to allow BC for attaching per-table prefixes, but it is meant for internal use only.

4 calls to Connection::__construct()
Connection::__construct in core/modules/sqlite/src/Driver/Database/sqlite/Connection.php
Constructs a \Drupal\sqlite\Driver\Database\sqlite\Connection object.
Connection::__construct in core/modules/mysql/src/Driver/Database/mysql/Connection.php
Constructs a Connection object.
Connection::__construct in core/modules/pgsql/src/Driver/Database/pgsql/Connection.php
Constructs a connection object.
StubConnection::__construct in core/tests/Drupal/Tests/Core/Database/Stub/StubConnection.php
Constructs a Connection object.
4 methods override Connection::__construct()
Connection::__construct in core/modules/sqlite/src/Driver/Database/sqlite/Connection.php
Constructs a \Drupal\sqlite\Driver\Database\sqlite\Connection object.
Connection::__construct in core/modules/mysql/src/Driver/Database/mysql/Connection.php
Constructs a Connection object.
Connection::__construct in core/modules/pgsql/src/Driver/Database/pgsql/Connection.php
Constructs a connection object.
StubConnection::__construct in core/tests/Drupal/Tests/Core/Database/Stub/StubConnection.php
Constructs a Connection object.

File

core/lib/Drupal/Core/Database/Connection.php, line 233

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

public function __construct(object $connection, array $connection_options) {
    if ($this->identifierQuotes === NULL) {
        @trigger_error('In drupal:10.0.0 not setting the $identifierQuotes property in the concrete Connection class will result in an RuntimeException. See https://www.drupal.org/node/2986894', E_USER_DEPRECATED);
        $this->identifierQuotes = [
            '',
            '',
        ];
    }
    assert(count($this->identifierQuotes) === 2 && Inspector::assertAllStrings($this->identifierQuotes), '\\Drupal\\Core\\Database\\Connection::$identifierQuotes must contain 2 string values');
    // The 'transactions' option is deprecated.
    if (isset($connection_options['transactions'])) {
        @trigger_error('Passing a \'transactions\' connection option to ' . __METHOD__ . ' is deprecated in drupal:9.1.0 and is removed in drupal:10.0.0. All database drivers must support transactions. See https://www.drupal.org/node/2278745', E_USER_DEPRECATED);
        unset($connection_options['transactions']);
    }
    // Manage the table prefix.
    if (isset($connection_options['prefix']) && is_array($connection_options['prefix'])) {
        if (count($connection_options['prefix']) > 1) {
            // If there are keys left besides the 'default' one, we are in a
            // multi-prefix scenario (for per-table prefixing, or migrations).
            // In that case, we put the non-default keys in a 'extra_prefix' key
            // to avoid mixing up with the normal 'prefix', which is a string since
            // Drupal 9.1.0.
            $prefix = $connection_options['prefix']['default'] ?? '';
            unset($connection_options['prefix']['default']);
            if (isset($connection_options['extra_prefix'])) {
                $connection_options['extra_prefix'] = array_merge($connection_options['extra_prefix'], $connection_options['prefix']);
            }
            else {
                $connection_options['extra_prefix'] = $connection_options['prefix'];
            }
        }
        else {
            $prefix = $connection_options['prefix']['default'] ?? '';
        }
        $connection_options['prefix'] = $prefix;
    }
    // Initialize and prepare the connection prefix.
    if (!isset($connection_options['extra_prefix'])) {
        $prefix = $connection_options['prefix'] ?? '';
    }
    else {
        $default_prefix = $connection_options['prefix'] ?? '';
        $prefix = $connection_options['extra_prefix'];
        $prefix['default'] = $default_prefix;
    }
    $this->setPrefix($prefix);
    // Work out the database driver namespace if none is provided. This normally
    // written to setting.php by installer or set by
    // \Drupal\Core\Database\Database::parseConnectionInfo().
    if (empty($connection_options['namespace'])) {
        $connection_options['namespace'] = (new \ReflectionObject($this))->getNamespaceName();
    }
    // The support for database drivers where the namespace that starts with
    // Drupal\\Driver\\Database\\ is deprecated.
    if (strpos($connection_options['namespace'], 'Drupal\\Driver\\Database') === 0) {
        @trigger_error('Support for database drivers located in the "drivers/lib/Drupal/Driver/Database" directory is deprecated in drupal:9.1.0 and is removed in drupal:10.0.0. Contributed and custom database drivers should be provided by modules and use the namespace "Drupal\\MODULE_NAME\\Driver\\Database\\DRIVER_NAME". See https://www.drupal.org/node/3123251', E_USER_DEPRECATED);
    }
    // Set a Statement class, unless the driver opted out.
    // @todo remove this in Drupal 10 https://www.drupal.org/node/3177490
    if (!empty($this->statementClass)) {
        @trigger_error('\\Drupal\\Core\\Database\\Connection::$statementClass is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Database drivers should use or extend StatementWrapper instead, and encapsulate client-level statement objects. See https://www.drupal.org/node/3177488', E_USER_DEPRECATED);
        $connection->setAttribute(\PDO::ATTR_STATEMENT_CLASS, [
            $this->statementClass,
            [
                $this,
            ],
        ]);
    }
    $this->connection = $connection;
    $this->connectionOptions = $connection_options;
}

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