diff --git a/src/Query/Sql/Select.php b/src/Query/Sql/Select.php index 78d5845..4f59cb8 100644 --- a/src/Query/Sql/Select.php +++ b/src/Query/Sql/Select.php @@ -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) { diff --git a/src/Translator/Mysql.php b/src/Translator/Mysql.php index 25ef3d9..0d85549 100644 --- a/src/Translator/Mysql.php +++ b/src/Translator/Mysql.php @@ -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 . ', '; } diff --git a/tests/Query/Sql/Select.php b/tests/Query/Sql/Select.php index 0c6a352..5063fd4 100644 --- a/tests/Query/Sql/Select.php +++ b/tests/Query/Sql/Select.php @@ -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 */ diff --git a/tests/Translator/Mysql.php b/tests/Translator/Mysql.php index 8703d84..0f8803f 100644 --- a/tests/Translator/Mysql.php +++ b/tests/Translator/Mysql.php @@ -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")); + }); } /**