function Connection::setPrefix

Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::setPrefix()
  2. 10 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::setPrefix()
  3. 10 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::setPrefix()
  4. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::setPrefix()
  5. 11.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::setPrefix()

Set the list of prefixes used by this database connection.

Parameters

array|string $prefix: Either a single prefix, or an array of prefixes.

3 calls to Connection::setPrefix()
Connection::queryTemporary in core/modules/sqlite/src/Driver/Database/sqlite/Connection.php
Runs a SELECT query and stores its results in a temporary table.
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/lib/Drupal/Core/Database/Connection.php
Constructs a Connection object.

File

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

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

protected function setPrefix($prefix) {
    if (is_array($prefix)) {
        $this->prefixes = $prefix + [
            'default' => '',
        ];
    }
    else {
        $this->prefixes = [
            'default' => $prefix,
        ];
    }
    [
        $start_quote,
        $end_quote,
    ] = $this->identifierQuotes;
    // Set up variables for use in prefixTables(). Replace table-specific
    // prefixes first.
    $this->prefixSearch = [];
    $this->prefixReplace = [];
    foreach ($this->prefixes as $key => $val) {
        if ($key != 'default') {
            $this->prefixSearch[] = '{' . $key . '}';
            // $val can point to another database like 'database.users'. In this
            // instance we need to quote the identifiers correctly.
            $val = str_replace('.', $end_quote . '.' . $start_quote, $val);
            $this->prefixReplace[] = $start_quote . $val . $key . $end_quote;
        }
    }
    // Then replace remaining tables with the default prefix.
    $this->prefixSearch[] = '{';
    // $this->prefixes['default'] can point to another database like
    // 'other_db.'. In this instance we need to quote the identifiers correctly.
    // For example, "other_db"."PREFIX_table_name".
    $this->prefixReplace[] = $start_quote . str_replace('.', $end_quote . '.' . $start_quote, $this->prefixes['default']);
    $this->prefixSearch[] = '}';
    $this->prefixReplace[] = $end_quote;
    // Set up a map of prefixed => un-prefixed tables.
    foreach ($this->prefixes as $table_name => $prefix) {
        if ($table_name !== 'default') {
            $this->unprefixedTablesMap[$prefix . $table_name] = $table_name;
        }
    }
}

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