You are here

public function Sql::getConnection in Drupal 9

Gets the database connection to use for the view.

The returned database connection does not have to be the default database connection. It can also be to another database connection when the view is to an external database or a replica database.

Return value

\Drupal\Core\Database\Connection The database connection to be used for the query.

1 call to Sql::getConnection()
Sql::buildCondition in core/modules/views/src/Plugin/views/query/Sql.php
Construct the "WHERE" or "HAVING" part of the query.

File

core/modules/views/src/Plugin/views/query/Sql.php, line 1263

Class

Sql
Views query plugin for an SQL query.

Namespace

Drupal\views\Plugin\views\query

Code

public function getConnection() {

  // Set the replica target if the replica option is set for the view.
  $target = empty($this->options['replica']) ? 'default' : 'replica';

  // Use an external database when the view configured to.
  $key = $this->view->base_database ?? 'default';
  return Database::getConnection($target, $key);
}