From 7bb72eb7c589ab48d1815039de583ea929667b75 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Mon, 25 Sep 2023 15:35:34 +1300 Subject: [PATCH] FIX Ensure all fixed fields are added --- src/ORM/DataQuery.php | 6 ++++- tests/php/ORM/DataQueryFixedFieldsTest.php | 30 ++++++++++++++++++++++ tests/php/ORM/DataQueryFixedFieldsTest.yml | 4 +++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/php/ORM/DataQueryFixedFieldsTest.php create mode 100644 tests/php/ORM/DataQueryFixedFieldsTest.yml diff --git a/src/ORM/DataQuery.php b/src/ORM/DataQuery.php index d11ee561fdb..868921cd8bf 100644 --- a/src/ORM/DataQuery.php +++ b/src/ORM/DataQuery.php @@ -207,7 +207,11 @@ public function getFinalisedQuery($queriedColumns = null) $queriedColumns = $this->queriedColumns; } if ($queriedColumns) { - $queriedColumns = array_merge($queriedColumns, ['Created', 'LastEdited', 'ClassName']); + // Add fixed fields to the query + // ID is a special case and gets added separately later + $fixedFields = DataObject::config()->uninherited('fixed_fields'); + unset($fixedFields['ID']); + $queriedColumns = array_merge($queriedColumns, array_keys($fixedFields)); } $query = clone $this->query; diff --git a/tests/php/ORM/DataQueryFixedFieldsTest.php b/tests/php/ORM/DataQueryFixedFieldsTest.php new file mode 100644 index 00000000000..440cf592f84 --- /dev/null +++ b/tests/php/ORM/DataQueryFixedFieldsTest.php @@ -0,0 +1,30 @@ +merge('fixed_fields', ['ExtraFixedField' => 'Varchar']); + static::tempDB()->resetDBSchema(static::$extra_dataobjects); + } + + public function testDataQueryHasFixedFields() + { + $dataQuery = new DataQuery(DataQueryTest\ObjectA::class); + $dataQuery->setQueriedColumns(['Name']); + $this->assertSame(['This is the field'], $dataQuery->execute()->column('ExtraFixedField')); + } +} diff --git a/tests/php/ORM/DataQueryFixedFieldsTest.yml b/tests/php/ORM/DataQueryFixedFieldsTest.yml new file mode 100644 index 00000000000..6e439226d4a --- /dev/null +++ b/tests/php/ORM/DataQueryFixedFieldsTest.yml @@ -0,0 +1,4 @@ +SilverStripe\ORM\Tests\DataQueryTest\ObjectA: + query1: + Name: 'Only one object' + ExtraFixedField: 'This is the field'