You are here

function views_many_to_one_helper::add_table in Views (for Drupal 7) 6.3

Same name and namespace in other branches
  1. 6.2 includes/handlers.inc \views_many_to_one_helper::add_table()
  2. 7.3 includes/handlers.inc \views_many_to_one_helper::add_table()

Add a table to the query.

This is an advanced concept; not only does it add a new instance of the table, but it follows the relationship path all the way down to the relationship link point and adds *that* as a new relationship and then adds the table to the relationship, if necessary.

2 calls to views_many_to_one_helper::add_table()
views_many_to_one_helper::ensure_my_table in includes/handlers.inc
Override ensure_my_table so we can control how this joins in. The operator actually has influence over joining.
views_many_to_one_helper::summary_join in includes/handlers.inc
Provide the proper join for summary queries. This is important in part because it will cooperate with other arguments if possible.

File

includes/handlers.inc, line 798
handlers.inc Defines the various handler objects to help build and display views.

Class

views_many_to_one_helper
This many to one helper object is used on both arguments and filters.

Code

function add_table($join = NULL, $alias = NULL) {

  // This is used for lookups in the many_to_one table.
  $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field;
  if (empty($join)) {
    $join = $this
      ->get_join();
  }

  // See if there's a chain between us and the base relationship. If so, we need
  // to create a new relationship to use.
  $relationship = $this->handler->relationship;

  // Determine the primary table to seek
  if (empty($this->handler->query->relationships[$relationship])) {
    $base_table = $this->handler->query->base_table;
  }
  else {
    $base_table = $this->handler->query->relationships[$relationship]['base'];
  }

  // Cycle through the joins. This isn't as error-safe as the normal
  // ensure_path logic. Perhaps it should be.
  $r_join = drupal_clone($join);
  while ($r_join->left_table != $base_table) {
    $r_join = views_get_table_join($r_join->left_table, $base_table);
  }

  // If we found that there are tables in between, add the relationship.
  if ($r_join->table != $join->table) {
    $relationship = $this->handler->query
      ->add_relationship($this->handler->table . '_' . $r_join->table, $r_join, $r_join->table, $this->handler->relationship);
  }

  // And now add our table, using the new relationship if one was used.
  $alias = $this->handler->query
    ->add_table($this->handler->table, $relationship, $join, $alias);

  // Store what values are used by this table chain so that other chains can
  // automatically discard those values.
  if (empty($this->handler->view->many_to_one_tables[$field])) {
    $this->handler->view->many_to_one_tables[$field] = $this->handler->value;
  }
  else {
    $this->handler->view->many_to_one_tables[$field] = array_merge($this->handler->view->many_to_one_tables[$field], $this->handler->value);
  }
  return $alias;
}