Skip to content

Commit

Permalink
Fixed: SQL order by ignoring raw expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Aug 19, 2016
1 parent 46d9e3a commit ab620fe
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Query/Sql/Select.php
Expand Up @@ -239,6 +239,10 @@ public function orderBy($columns, $direction = 'asc')
{
$columns = $this->stringArgumentToArray($columns);
}
elseif ($columns instanceof Expression)
{
$this->orders[] = [$columns, $direction]; return $this;
}

foreach ($columns as $key => $column)
{
Expand Down
8 changes: 8 additions & 0 deletions src/Translator/Mysql.php
Expand Up @@ -615,6 +615,14 @@ protected function translateOrderBy()

foreach ($this->attr('orders') as $column => $direction)
{
// in case a raw value is given we had to
// put the column / raw value an direction inside another
// array because we cannot make objects to array keys.
if (is_array($direction))
{
list($column, $direction) = $direction;
}

$build .= $this->escape($column) . ' ' . $direction . ', ';
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Query/Sql/Select.php
Expand Up @@ -119,6 +119,17 @@ public function testOrderBy()
$this->assertAttributes($this->createQuery()->orderBy(array('firstname' => 'asc', 'lastname' => 'desc')), array('orders' => array('firstname' => 'asc', 'lastname' => 'desc')));
}

/**
* Select::orderBy
*/
public function testOrderByRaw()
{
$raw = new Expression('language <> de');

// simple
$this->assertAttributes($this->createQuery()->orderBy($raw), array('orders' => array([$raw, 'asc'])));
}

/**
* Select::groupBy
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/Translator/Mysql.php
Expand Up @@ -362,6 +362,12 @@ public function testSelectOrderBy()
{
return $q->table('phpunit')->select()->orderBy(array('u.firstname' => 'asc', 'u.lastname' => 'desc'));
});

// raw sorting
$this->assertQueryTranslation('select * from `phpunit` order by firstname <> nick asc', array(), function($q)
{
return $q->table('phpunit')->select()->orderBy(new Expression("firstname <> nick"));
});
}

/**
Expand Down

0 comments on commit ab620fe

Please sign in to comment.