diff --git a/src/ORM/DataQuery.php b/src/ORM/DataQuery.php index a8241ae61c4..a449b3e8649 100644 --- a/src/ORM/DataQuery.php +++ b/src/ORM/DataQuery.php @@ -212,7 +212,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'