You are here

public function views_plugin_query_default::add_relationship in Views (for Drupal 7) 7.3

Same name and namespace in other branches
  1. 6.3 plugins/views_plugin_query_default.inc \views_plugin_query_default::add_relationship()

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

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

views_join $join: A views_join object (or derived object) to join the alias in.

string $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.

string $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

plugins/views_plugin_query_default.inc, line 346
Definition of views_plugin_query_default.

Class

views_plugin_query_default
Object used to create a SELECT query.

Code

public function add_relationship($alias, $join, $base, $link_point = NULL) {
  if (empty($link_point)) {
    $link_point = $this->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
      ->adjust_join($join, $link_point);
  }

  // Add the table directly to the queue to avoid accidentally marking it.
  $this->table_queue[$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->base_table][$alias] = array(
    'count' => 1,
    'alias' => $alias,
  );
  return $alias;
}