You are here

public function Sql::addRelationship in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Plugin/views/query/Sql.php \Drupal\views\Plugin\views\query\Sql::addRelationship()

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 nodereference table. If you have a nodereference named 'book_parent' which links to a parent node, you could set up a relationship 'node_book_parent' to 'node'. Then, anything that links to 'node' can link to 'node_book_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 277
Contains \Drupal\views\Plugin\views\query\Sql.

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] = array(
    'table' => $join->table,
    'num' => 1,
    'alias' => $alias,
    'join' => $join,
    'relationship' => $link_point,
  );
  $this->relationships[$alias] = array(
    'link' => $link_point,
    'table' => $join->table,
    'base' => $base,
  );
  $this->tables[$this->view->storage
    ->get('base_table')][$alias] = array(
    'count' => 1,
    'alias' => $alias,
  );
  return $alias;
}