You are here

public function SelectQuery::addJoin in Drupal 7

Join against another table in the database.

This method does the "hard" work of queuing up a table to be joined against. In some cases, that may include dipping into the Schema API to find the necessary fields on which to join.

Parameters

$type: The type of join. Typically one one of INNER, LEFT OUTER, and RIGHT OUTER.

$table: The table against which to join. May be a string or another SelectQuery object. If a query object is passed, it will be used as a subselect.

$alias: The alias for the table. In most cases this should be the first letter of the table, or the first letter of each "word" in the table. If omitted, one will be dynamically generated.

$condition: The condition on which to join this table. If the join requires values, this clause should use a named placeholder and the value or values to insert should be passed in the 4th parameter. For the first table joined on a query, this value is ignored as the first table is taken as the base table. The token %alias can be used in this string to be replaced with the actual alias. This is useful when $alias is modified by the database system, for example, when joining the same table more than once.

$arguments: An array of arguments to replace into the $condition of this join.

Return value

The unique alias that was assigned for this table.

Overrides SelectQueryInterface::addJoin

5 calls to SelectQuery::addJoin()
SelectQuery::innerJoin in includes/database/select.inc
Inner Join against another table in the database.
SelectQuery::join in includes/database/select.inc
Default Join against another table in the database.
SelectQuery::leftJoin in includes/database/select.inc
Left Outer Join against another table in the database.
SelectQuery::rightJoin in includes/database/select.inc
Right Outer Join against another table in the database.
SelectQuery::__construct in includes/database/select.inc
Constructs a Query object.

File

includes/database/select.inc, line 1369

Class

SelectQuery
Query builder for SELECT statements.

Code

public function addJoin($type, $table, $alias = NULL, $condition = NULL, $arguments = array()) {
  if (empty($alias)) {
    if ($table instanceof SelectQueryInterface) {
      $alias = 'subquery';
    }
    else {
      $alias = $table;
    }
  }
  $alias_candidate = $alias;
  $count = 2;
  while (!empty($this->tables[$alias_candidate])) {
    $alias_candidate = $alias . '_' . $count++;
  }
  $alias = $alias_candidate;
  if (is_string($condition)) {
    $condition = str_replace('%alias', $alias, $condition);
  }
  $this->tables[$alias] = array(
    'join type' => $type,
    'table' => $table,
    'alias' => $alias,
    'condition' => $condition,
    'arguments' => $arguments,
  );
  return $alias;
}