Merge pull request #10173 from GuySartorelli/patch-2

FIX Allow custom SELECT to be used for sorting in DataQuery::column()
This commit is contained in:
Michal Kleiner 2021-12-14 10:23:48 +13:00 committed by GitHub
commit 645e1f14bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -395,7 +395,12 @@ class DataQuery
// 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) . '"';

View File

@ -326,6 +326,21 @@ class DataQueryTest extends SapphireTest
);
}
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')");