From 9b92d5b1346436e42ec9048bb0cc3c24c4d60adf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Do=CC=88ring?= Date: Mon, 4 Feb 2019 10:42:10 +0100 Subject: [PATCH] Fixed order expression array --- src/Query/Sql/Select.php | 9 +++++++-- tests/Builder.php | 10 +--------- tests/Query/Sql/Select.php | 11 ++++++++++- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Query/Sql/Select.php b/src/Query/Sql/Select.php index 819917c..521a6cd 100644 --- a/src/Query/Sql/Select.php +++ b/src/Query/Sql/Select.php @@ -276,12 +276,17 @@ public function orderBy($columns, $direction = 'asc') { $this->orders[] = array($columns, $direction); return $this; } - + foreach ($columns as $key => $column) { if (is_numeric($key)) { - $this->orders[$column] = $direction; + if ($column instanceof Expression) + { + $this->orders[] = array($column, $direction); + } else { + $this->orders[$column] = $direction; + } } else { $this->orders[$key] = $column; } diff --git a/tests/Builder.php b/tests/Builder.php index 04e130b..d3edfbb 100644 --- a/tests/Builder.php +++ b/tests/Builder.php @@ -14,14 +14,6 @@ class Builder_Test extends \PHPUnit\Framework\TestCase { - /** - * Builder::extend test - */ - public function testExtend() - { - Builder::extend('phpunit', '\\This\\Should\\Work', '\\Without\\The\\Class\\Existing'); - } - /** * Builder::extend test * @@ -130,4 +122,4 @@ public function testQueryClassesAsExpected() $hydrahon->table('test')->insert(['foo' => 'bar'])->execute(); } -} \ No newline at end of file +} diff --git a/tests/Query/Sql/Select.php b/tests/Query/Sql/Select.php index 75a73a6..89c96d5 100644 --- a/tests/Query/Sql/Select.php +++ b/tests/Query/Sql/Select.php @@ -128,6 +128,15 @@ public function testOrderByRaw() // simple $this->assertAttributes($this->createQuery()->orderBy($raw), array('orders' => array(array($raw, 'asc')))); + + // in array + $this->assertAttributes($this->createQuery()->orderBy([$raw, $raw]), array('orders' => array(array($raw, 'asc'), array($raw, 'asc')))); + + // in array order + $this->assertAttributes($this->createQuery()->orderBy([$raw, $raw], 'desc'), array('orders' => array(array($raw, 'desc'), array($raw, 'desc')))); + + // in array mixing + $this->assertAttributes($this->createQuery()->orderBy([$raw, 'foo' => 'asc'], 'desc'), array('orders' => array(array($raw, 'desc'), 'foo' => 'asc'))); } /** @@ -304,4 +313,4 @@ public function testGroupResults() $this->assertEquals('Johnna', $grouped[22][5]['name']); $this->assertEquals('Johnna', $grouped[20][2]['name']); } -} \ No newline at end of file +}