function Sql::addRelationship

Same name in other branches
  1. 9 core/modules/views/src/Plugin/views/query/Sql.php \Drupal\views\Plugin\views\query\Sql::addRelationship()
  2. 8.9.x core/modules/views/src/Plugin/views/query/Sql.php \Drupal\views\Plugin\views\query\Sql::addRelationship()
  3. 10 core/modules/views/src/Plugin/views/query/Sql.php \Drupal\views\Plugin\views\query\Sql::addRelationship()

Adds a relationship to the query.

A relationship is an alternative endpoint to a series of table joins. Relationships must be aliases of the primary table and they must join either to the primary table or to a pre-existing relationship.

An example of a relationship would be a node reference table. If you have a node reference named 'content_parent' which links to a parent node, you could set up a relationship 'node_content_parent' to 'node'. Then, anything that links to 'node' can link to 'node_content_parent' instead, thus allowing all properties of both nodes to be available in the query.

Parameters

$alias: What this relationship will be called, and is also the alias for the table.

\Drupal\views\Plugin\views\join\JoinPluginBase $join: A Join object (or derived object) to join the alias in.

$base: The name of the 'base' table this relationship represents; this tells the join search which path to attempt to use when finding the path to this relationship.

$link_point: If this relationship links to something other than the primary table, specify that table here. For example, a 'track' node might have a relationship to an 'album' node, which might have a relationship to an 'artist' node.

File

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

Class

Sql
Views query plugin for an SQL query.

Namespace

Drupal\views\Plugin\views\query

Code

public function addRelationship($alias, JoinPluginBase $join, $base, $link_point = NULL) {
    if (empty($link_point)) {
        $link_point = $this->view->storage
            ->get('base_table');
    }
    elseif (!array_key_exists($link_point, $this->relationships)) {
        return FALSE;
    }
    // Make sure $alias isn't already used; if it, start adding stuff.
    $alias_base = $alias;
    $count = 1;
    while (!empty($this->relationships[$alias])) {
        $alias = $alias_base . '_' . $count++;
    }
    // Make sure this join is adjusted for our relationship.
    if ($link_point && isset($this->relationships[$link_point])) {
        $join = $this->adjustJoin($join, $link_point);
    }
    // Add the table directly to the queue to avoid accidentally marking
    // it.
    $this->tableQueue[$alias] = [
        'table' => $join->table,
        'num' => 1,
        'alias' => $alias,
        'join' => $join,
        'relationship' => $link_point,
    ];
    $this->relationships[$alias] = [
        'link' => $link_point,
        'table' => $join->table,
        'base' => $base,
    ];
    $this->tables[$this->view->storage
        ->get('base_table')][$alias] = [
        'count' => 1,
        'alias' => $alias,
    ];
    return $alias;
}

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