diff --git a/model/SQLQuery.php b/model/SQLQuery.php index 6e49d9b7f..38a672e20 100644 --- a/model/SQLQuery.php +++ b/model/SQLQuery.php @@ -1127,10 +1127,15 @@ class SQLQuery { // shift the first FROM table out from so we only deal with the JOINs $baseFrom = array_shift($from); $this->mergesort($from, function($firstJoin, $secondJoin) { - if($firstJoin['order'] == $secondJoin['order']) { + if( + !is_array($firstJoin) + || !is_array($secondJoin) + || $firstJoin['order'] == $secondJoin['order'] + ) { return 0; + } else { + return ($firstJoin['order'] < $secondJoin['order']) ? -1 : 1; } - return ($firstJoin['order'] < $secondJoin['order']) ? -1 : 1; }); // Put the first FROM table back into the results diff --git a/tests/model/SQLQueryTest.php b/tests/model/SQLQueryTest.php index de68aef7b..d69908ba0 100755 --- a/tests/model/SQLQueryTest.php +++ b/tests/model/SQLQueryTest.php @@ -132,10 +132,21 @@ class SQLQueryTest extends SapphireTest { $query = new SQLQuery(); $query->setFrom("MyTable"); $query->setOrderBy('RAND()'); - $this->assertEquals( 'SELECT *, RAND() AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" ASC', $query->sql()); + + $query = new SQLQuery(); + $query->setFrom("MyTable"); + $query->addFrom('INNER JOIN SecondTable USING (ID)'); + $query->addFrom('INNER JOIN ThirdTable USING (ID)'); + $query->setOrderBy('MyName'); + $this->assertEquals( + 'SELECT * FROM MyTable ' + . 'INNER JOIN SecondTable USING (ID) ' + . 'INNER JOIN ThirdTable USING (ID) ' + . 'ORDER BY MyName ASC', + $query->sql()); } public function testNullLimit() {