Skip to content

Commit

Permalink
Merge pull request #10173 from GuySartorelli/patch-2
Browse files Browse the repository at this point in the history
FIX Allow custom SELECT to be used for sorting in DataQuery::column()
  • Loading branch information
michalkleiner authored Dec 13, 2021
2 parents 63ec506 + 0b0c137 commit 645e1f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ORM/DataQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,12 @@ protected function ensureSelectContainsOrderbyColumns($query, $originalSelect =
// format internally; then this check can be part of selectField()
$selects = $query->getSelect();
if (!isset($selects[$col]) && !in_array($qualCol, $selects)) {
$query->selectField($qualCol);
// Use the original select if possible.
if (array_key_exists($col, $originalSelect)) {
$query->selectField($originalSelect[$col], $col);
} else {
$query->selectField($qualCol);
}
}
} else {
$qualCol = '"' . implode('"."', $parts) . '"';
Expand Down
15 changes: 15 additions & 0 deletions tests/php/ORM/DataQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,21 @@ public function testSurrogateFieldSort()
);
}

public function testCustomFieldWithAliasSort()
{
$query = new DataQuery(DataQueryTest\ObjectE::class);
$query->selectField(sprintf(
'(case when "Title" = %s then 1 else 0 end)',
DB::get_conn()->quoteString('Second')
), 'CustomColumn');
$query->sort('CustomColumn', 'DESC', true);
$query->sort('SortOrder', 'ASC', false);
$this->assertEquals(
['Second', 'First', 'Last'],
$query->column('Title')
);
}

public function testComparisonClauseDateStartsWith()
{
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')");
Expand Down

0 comments on commit 645e1f1

Please sign in to comment.