Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Set tableName on DBField before calling addToQuery #10946

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ORM/DataQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ protected function selectColumnsFromTable(SQLSelect &$query, $tableClass, $colum
$schema = DataObject::getSchema();
$databaseFields = $schema->databaseFields($tableClass, false);
$compositeFields = $schema->compositeFields($tableClass, false);
$tableName = $schema->tableName($tableClass);
unset($databaseFields['ID']);
foreach ($databaseFields as $k => $v) {
if ((is_null($columns) || in_array($k, $columns ?? [])) && !isset($compositeFields[$k])) {
Expand All @@ -618,12 +619,12 @@ protected function selectColumnsFromTable(SQLSelect &$query, $tableClass, $colum
$query->selectField($quotedField, $k);
}
$dbO = Injector::inst()->create($v, $k);
$dbO->setTable($tableName);
$dbO->addToQuery($query);
}
}
foreach ($compositeFields as $k => $v) {
if ((is_null($columns) || in_array($k, $columns ?? [])) && $v) {
$tableName = $schema->tableName($tableClass);
$dbO = Injector::inst()->create($v, $k);
$dbO->setTable($tableName);
$dbO->addToQuery($query);
Expand Down
5 changes: 3 additions & 2 deletions tests/php/ORM/DataQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,9 @@ public function testAddToQueryIsCalled()
// Including filter on parent table only doesn't pull in second
$query = new DataQuery(DataQueryTest\DataObjectAddsToQuery::class);
$result = $query->getFinalisedQuery();
// The `DBFieldAddsToQuery` test field removes itself from the select query
$this->assertArrayNotHasKey('FieldTwo', $result->getSelect());
// The `DBFieldAddsToQuery` test field adds a new field to the select query
$this->assertArrayHasKey('FieldTwo2', $result->getSelect());
$this->assertSame('"DataQueryTest_AddsToQuery"."FieldTwo"', $result->getSelect()['FieldTwo2']);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions tests/php/ORM/DataQueryTest/DBFieldAddsToQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\FieldType\DBText;
use SilverStripe\ORM\Queries\SQLSelect;

class DBFieldAddsToQuery extends DBText implements TestOnly
{
public function addToQuery(&$query)
{
$select = $query->getSelect();
unset($select[$this->name]);
$query->setSelect($select);
// Add a new item, to validate that tableName and name are set correctly.
/** @var SQLSelect $query */
$query->addSelect([$this->name . '2' => '"' . $this->tableName . '"."' . $this->name . '"']);
}
}