/** * Set a column to order by and the direction to do so. * Update 2013-12-06 : originally Field only, didn't make sense not to * use a string as well because the order is supposed to specific to this * table. * @param mixed $column * @param string $direction Either ASC or DESC */ public function addOrderBy($column, $direction = 'ASC') { if (!$column instanceof \Database\Field && is_string($column)) { $column = $this->getField($column); } static $allowed_directions = array('ASC', 'DESC', 'RAND', 'RANDOM'); $direction = trim(strtoupper($direction)); if (!in_array($direction, $allowed_directions)) { throw new \Exception(t('Unknown order direction: %s', $direction)); } // If a random call, return the db os specific function call. if ($direction == 'RAND' || $direction == 'RANDOM') { $direction = $this->db->getRandomCall(); } if (DATABASE_CHECK_COLUMNS && !$this->columnExists($column)) { throw new \Exception(t('Table column "%s" is not known', $column)); } else { if ($column->hasAlias()) { $this->orders[] = $column->getAlias() . " {$direction}"; } else { $this->orders[] = $column->getFullName() . " {$direction}"; } } return $this; }