Only compare array notations in SQLQuery->getOrderedJoins()

If more than two $from were added through SQLQuery->addFrom(),
the getOrderedJoins() comparison kicks in. It assumes all $from
parts are in array notation, which isn't always the case.
For legacy reasons, and because we don't have full API support,
you can still add literal joins through addFrom('INNER JOIN ...').
On PHP 5.3, the ordering comparison still works because it
allows array access in strings, with string rather than numeric indexes.
Thankfully that's no longer supported in PHP 5.4.
This commit is contained in:
Ingo Schommer 2013-09-17 22:06:39 +02:00
parent f07ce0b25e
commit ba5984e2bf
2 changed files with 19 additions and 3 deletions

View File

@ -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

View File

@ -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() {