function views_query::add_orderby

2.x query.inc views_query::add_orderby($table, $field, $order, $alias = '')

Add an ORDER BY clause to the query.

Parameters

$table: The table this field is part of. If a formula, enter NULL.

$field: The field or formula to sort on. If already a field, enter NULL and put in the alias.

$order: Either ASC or DESC.

$alias: The alias to add the field as. In SQL, all fields in the order by must also be in the SELECT portion. If an $alias isn't specified one will be generated for from the $field; however, if the $field is a formula, this alias will likely fail.

File

includes/query.inc, line 818
query.inc Defines the query object which is the underlying layer in a View.

Class

views_query
Object used to create a SELECT query.

Code

function add_orderby($table, $field, $order, $alias = '') {
  if ($table) {
    $this->ensure_table($table);
  }

  // Only fill out this aliasing if there is a table;
  // otherwise we assume it is a formula.
  if (!$alias && $table) {
    $as = $table . '_' . $field;
  }
  else {
    $as = $alias;
  }

  if ($field) {
    $this->add_field($table, $field, $as);
  }

  $this->orderby[] = "$as " . strtoupper($order);

  // If grouping, all items in the order by must also be in the
  // group by clause. Check $table to ensure that this is not a
  // formula.
  if ($this->groupby && $table) {
    $this->add_groupby($as);
  }
}