mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #5579 from tractorcow/pulls/3.3/fix-finalised-query
BUG Fix getFinalisedQuery not including all queried columns
This commit is contained in:
commit
4afdb630b5
@ -170,7 +170,9 @@ class DataQuery {
|
||||
* @return SQLQuery The finalised sql query
|
||||
*/
|
||||
public function getFinalisedQuery($queriedColumns = null) {
|
||||
if(!$queriedColumns) $queriedColumns = $this->queriedColumns;
|
||||
if(!$queriedColumns) {
|
||||
$queriedColumns = $this->queriedColumns;
|
||||
}
|
||||
if($queriedColumns) {
|
||||
$queriedColumns = array_merge($queriedColumns, array('Created', 'LastEdited', 'ClassName'));
|
||||
}
|
||||
@ -185,11 +187,19 @@ class DataQuery {
|
||||
// Specifying certain columns allows joining of child tables
|
||||
$tableClasses = ClassInfo::dataClassesFor($this->dataClass);
|
||||
|
||||
// Ensure that any filtered columns are included in the selected columns
|
||||
foreach ($query->getWhereParameterised($parameters) as $where) {
|
||||
// Check for just the column, in the form '"Column" = ?' and the form '"Table"."Column"' = ?
|
||||
if (preg_match('/^"([^"]+)"/', $where, $matches) ||
|
||||
preg_match('/^"([^"]+)"\."[^"]+"/', $where, $matches)) {
|
||||
if (!in_array($matches[1], $queriedColumns)) $queriedColumns[] = $matches[1];
|
||||
// Check for any columns in the form '"Column" = ?' or '"Table"."Column"' = ?
|
||||
if(preg_match_all(
|
||||
'/(?:"(?<table>[^"]+)"\.)?"(?<column>[^"]+)"(?:[^\.]|$)/',
|
||||
$where, $matches, PREG_SET_ORDER
|
||||
)) {
|
||||
foreach($matches as $match) {
|
||||
$column = $match['column'];
|
||||
if (!in_array($column, $queriedColumns)) {
|
||||
$queriedColumns[] = $column;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -277,6 +277,45 @@ class DataQueryTest extends SapphireTest {
|
||||
$this->resetDBSchema(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that getFinalisedQuery can include all tables
|
||||
*/
|
||||
public function testConditionsIncludeTables() {
|
||||
// Including filter on parent table only doesn't pull in second
|
||||
$query = new DataQuery('DataQueryTest_C');
|
||||
$query->sort('"SortOrder"');
|
||||
$query->where(array(
|
||||
'"DataQueryTest_C"."Title" = ?' => array('First')
|
||||
));
|
||||
$result = $query->getFinalisedQuery(array('Title'));
|
||||
$from = $result->getFrom();
|
||||
$this->assertContains('DataQueryTest_C', array_keys($from));
|
||||
$this->assertNotContains('DataQueryTest_E', array_keys($from));
|
||||
|
||||
// Including filter on sub-table requires it
|
||||
$query = new DataQuery('DataQueryTest_C');
|
||||
$query->sort('"SortOrder"');
|
||||
$query->where(array(
|
||||
'"DataQueryTest_C"."Title" = ? OR "DataQueryTest_E"."SortOrder" > ?' => array(
|
||||
'First', 2
|
||||
)
|
||||
));
|
||||
$result = $query->getFinalisedQuery(array('Title'));
|
||||
$from = $result->getFrom();
|
||||
|
||||
// Check that including "SortOrder" prompted inclusion of DataQueryTest_E table
|
||||
$this->assertContains('DataQueryTest_C', array_keys($from));
|
||||
$this->assertContains('DataQueryTest_E', array_keys($from));
|
||||
$arrayResult = iterator_to_array($result->execute());
|
||||
$first = array_shift($arrayResult);
|
||||
$this->assertNotNull($first);
|
||||
$this->assertEquals('First', $first['Title']);
|
||||
$second = array_shift($arrayResult);
|
||||
$this->assertNotNull($second);
|
||||
$this->assertEquals('Last', $second['Title']);
|
||||
$this->assertEmpty(array_shift($arrayResult));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user