diff --git a/core/model/SQLQuery.php b/core/model/SQLQuery.php index 74dbb98ab..42983839d 100755 --- a/core/model/SQLQuery.php +++ b/core/model/SQLQuery.php @@ -455,6 +455,8 @@ class SQLQuery { * "false" more often that is strictly necessary. */ function canSortBy($fieldName) { + $fieldName = preg_replace('/(\s+?)(A|DE)SC$/', '', $fieldName); + $sql = $this->sql(); $selects = $this->select; diff --git a/tests/SQLQueryTest.php b/tests/SQLQueryTest.php index 4ab7ce032..dd921c0b3 100644 --- a/tests/SQLQueryTest.php +++ b/tests/SQLQueryTest.php @@ -50,6 +50,13 @@ class SQLQueryTest extends SapphireTest { $this->assertEquals("SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test')", $query->sql()); } + function testCanSortBy() { + $query = new SQLQuery(); + $query->select("Name","Meta")->from("MyTable")->where("Name", "Name")->where("Meta", "Test"); + $this->assertTrue($query->canSortBy('Name ASC')); + $this->assertTrue($query->canSortBy('Name')); + } + function testSelectWithChainedFilterParameters() { $query = new SQLQuery(); $query->select(array("Name","Meta"))->from("MyTable");