From 17733aa84ad4852bd5a5e8b157790d0993485d9c Mon Sep 17 00:00:00 2001 From: Rastislav Brandobur Date: Mon, 11 Sep 2023 10:58:11 +0200 Subject: [PATCH 1/2] added missing returns (fluent setter) --- src/ORM/FieldType/DBClassName.php | 2 ++ src/ORM/FieldType/DBPrimaryKey.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/ORM/FieldType/DBClassName.php b/src/ORM/FieldType/DBClassName.php index 0b59435934b..48229afda0b 100644 --- a/src/ORM/FieldType/DBClassName.php +++ b/src/ORM/FieldType/DBClassName.php @@ -151,6 +151,8 @@ public function setValue($value, $record = null, $markChanged = true) if ($record instanceof DataObject) { $this->record = $record; } + + return $this; } public function getDefault() diff --git a/src/ORM/FieldType/DBPrimaryKey.php b/src/ORM/FieldType/DBPrimaryKey.php index 13c047a5e82..8132bfa3f91 100644 --- a/src/ORM/FieldType/DBPrimaryKey.php +++ b/src/ORM/FieldType/DBPrimaryKey.php @@ -68,5 +68,7 @@ public function setValue($value, $record = null, $markChanged = true) if ($record instanceof DataObject) { $this->object = $record; } + + return $this; } } From 58696e3ed5105e81245c30ddb7d04c3560ac2148 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Thu, 14 Sep 2023 11:01:36 +1200 Subject: [PATCH 2/2] FIX Set tableName on DBField before calling addToQuery (#10946) --- src/ORM/DataQuery.php | 3 ++- tests/php/ORM/DataQueryTest.php | 5 +++-- tests/php/ORM/DataQueryTest/DBFieldAddsToQuery.php | 7 ++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/ORM/DataQuery.php b/src/ORM/DataQuery.php index 6f59d6bbf1f..d11ee561fdb 100644 --- a/src/ORM/DataQuery.php +++ b/src/ORM/DataQuery.php @@ -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])) { @@ -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); diff --git a/tests/php/ORM/DataQueryTest.php b/tests/php/ORM/DataQueryTest.php index de4677321d3..ff48d6311d9 100644 --- a/tests/php/ORM/DataQueryTest.php +++ b/tests/php/ORM/DataQueryTest.php @@ -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']); } /** diff --git a/tests/php/ORM/DataQueryTest/DBFieldAddsToQuery.php b/tests/php/ORM/DataQueryTest/DBFieldAddsToQuery.php index b6978687ddf..2471f0ad256 100644 --- a/tests/php/ORM/DataQueryTest/DBFieldAddsToQuery.php +++ b/tests/php/ORM/DataQueryTest/DBFieldAddsToQuery.php @@ -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 . '"']); } }