From 7ea25bf9e1b9e2af12f10fe794a5abb88f220b70 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Tue, 5 Sep 2023 15:06:53 +1200 Subject: [PATCH] FIX Call addToQuery on all DBFields (#10936) --- src/ORM/DataQuery.php | 2 ++ tests/php/ORM/DataQueryTest.php | 10 ++++++++++ tests/php/ORM/DataQueryTest.yml | 5 +++++ .../php/ORM/DataQueryTest/DBFieldAddsToQuery.php | 16 ++++++++++++++++ .../ORM/DataQueryTest/DataObjectAddsToQuery.php | 16 ++++++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 tests/php/ORM/DataQueryTest/DBFieldAddsToQuery.php create mode 100644 tests/php/ORM/DataQueryTest/DataObjectAddsToQuery.php diff --git a/src/ORM/DataQuery.php b/src/ORM/DataQuery.php index 89e9dae74..6f59d6bbf 100644 --- a/src/ORM/DataQuery.php +++ b/src/ORM/DataQuery.php @@ -617,6 +617,8 @@ class DataQuery } else { $query->selectField($quotedField, $k); } + $dbO = Injector::inst()->create($v, $k); + $dbO->addToQuery($query); } } foreach ($compositeFields as $k => $v) { diff --git a/tests/php/ORM/DataQueryTest.php b/tests/php/ORM/DataQueryTest.php index bbfa2ad82..de4677321 100644 --- a/tests/php/ORM/DataQueryTest.php +++ b/tests/php/ORM/DataQueryTest.php @@ -18,6 +18,7 @@ class DataQueryTest extends SapphireTest protected static $fixture_file = 'DataQueryTest.yml'; protected static $extra_dataobjects = [ + DataQueryTest\DataObjectAddsToQuery::class, DataQueryTest\ObjectA::class, DataQueryTest\ObjectB::class, DataQueryTest\ObjectC::class, @@ -381,6 +382,15 @@ class DataQueryTest extends SapphireTest static::resetDBSchema(true); } + 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()); + } + /** * Tests that getFinalisedQuery can include all tables */ diff --git a/tests/php/ORM/DataQueryTest.yml b/tests/php/ORM/DataQueryTest.yml index 917175cc2..e6e8dc177 100644 --- a/tests/php/ORM/DataQueryTest.yml +++ b/tests/php/ORM/DataQueryTest.yml @@ -36,3 +36,8 @@ SilverStripe\ORM\Tests\DataQueryTest\ObjectH: SortOrder: 2 ManyTestEs: =>SilverStripe\ORM\Tests\DataQueryTest\ObjectE.query2 ManyTestIs: =>SilverStripe\ORM\Tests\DataQueryTest\ObjectI.query3 + +SilverStripe\ORM\Tests\DataQueryTest\DataObjectAddsToQuery: + obj1: + FieldOne: 'This is a value' + FieldTwo: 'This is also a value' diff --git a/tests/php/ORM/DataQueryTest/DBFieldAddsToQuery.php b/tests/php/ORM/DataQueryTest/DBFieldAddsToQuery.php new file mode 100644 index 000000000..b6978687d --- /dev/null +++ b/tests/php/ORM/DataQueryTest/DBFieldAddsToQuery.php @@ -0,0 +1,16 @@ +getSelect(); + unset($select[$this->name]); + $query->setSelect($select); + } +} diff --git a/tests/php/ORM/DataQueryTest/DataObjectAddsToQuery.php b/tests/php/ORM/DataQueryTest/DataObjectAddsToQuery.php new file mode 100644 index 000000000..d5e5fda15 --- /dev/null +++ b/tests/php/ORM/DataQueryTest/DataObjectAddsToQuery.php @@ -0,0 +1,16 @@ + 'Text', + 'FieldTwo' => DBFieldAddsToQuery::class, + ]; +}