mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FIX allow custom SELECT to be used for sorting in DataQuery::column().
If a custom select clause (using special features such as `CASE`) is used, and it was added using `SQLSelect::selectField`, the custom select clause should be retained when calling DataQuery::column().
This commit is contained in:
parent
01600086c9
commit
0b0c13764b
@ -395,8 +395,13 @@ class DataQuery
|
|||||||
// format internally; then this check can be part of selectField()
|
// format internally; then this check can be part of selectField()
|
||||||
$selects = $query->getSelect();
|
$selects = $query->getSelect();
|
||||||
if (!isset($selects[$col]) && !in_array($qualCol, $selects)) {
|
if (!isset($selects[$col]) && !in_array($qualCol, $selects)) {
|
||||||
|
// Use the original select if possible.
|
||||||
|
if (array_key_exists($col, $originalSelect)) {
|
||||||
|
$query->selectField($originalSelect[$col], $col);
|
||||||
|
} else {
|
||||||
$query->selectField($qualCol);
|
$query->selectField($qualCol);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$qualCol = '"' . implode('"."', $parts) . '"';
|
$qualCol = '"' . implode('"."', $parts) . '"';
|
||||||
|
|
||||||
|
@ -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()
|
public function testComparisonClauseDateStartsWith()
|
||||||
{
|
{
|
||||||
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')");
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')");
|
||||||
|
Loading…
Reference in New Issue
Block a user