Skip to content

Commit

Permalink
NormaFind::OrderBy() should accept pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
alanszlosek committed Oct 11, 2014
1 parent d04066a commit 67b5b35
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Norma/Norma.php
Expand Up @@ -431,11 +431,20 @@ public function __call($name, $args)
return $this;
}

public function OrderBy($alias, $direction)
// Handle multiple pairs
public function OrderBy($pairs)
{
$pairs = func_get_args();
if (count($pairs) % 2 != 0) {
throw new \Exception('NormaFind: OrderBy needs pairs');
}
$className = $this->className;
$direction = (strtolower($direction) == 'asc' ? 'asc' : 'desc');
$this->_orderBy = array(\Norma\Norma::PrepareField($className::Table(), $className::AliasToField($alias)), $direction);
$out = array();
for ($i = 0; $i < count($pairs); $i+=2) {
$field = $pairs[$i];
$out[] = \Norma\Norma::PrepareField($className::Table(), $className::AliasToField($field)) . ' ' . (strtoupper($pairs[$i+1]) == 'ASC' ? 'ASC' : 'DESC');
}
$this->_orderBy = $out;

return $this;
}
Expand Down Expand Up @@ -524,7 +533,7 @@ public function MakeQueryParts()
$sql = '';
}

if ($this->_orderBy) $sql .= ' ORDER BY ' . implode(' ', $this->_orderBy);
if ($this->_orderBy) $sql .= ' ORDER BY ' . implode(',', $this->_orderBy);
if ($this->_limit) $sql .= ' LIMIT ' . $this->_limit;
if ($sql) {
$parts[] = $sql;
Expand Down

0 comments on commit 67b5b35

Please sign in to comment.