diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php index bb82bc364..613dc1411 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -2721,6 +2721,11 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity return null; } + if (!isset($this->record[$fieldName]) && isset($this->record[$fieldName . '_Lazy'])) { + $tableClass = $this->record[$fieldName . '_Lazy']; + $this->loadLazyFields($tableClass); + } + $value = isset($this->record[$fieldName]) ? $this->record[$fieldName] : null; diff --git a/tests/php/ORM/DataObjectLazyLoadingTest.php b/tests/php/ORM/DataObjectLazyLoadingTest.php index 5e178a517..fdfeed5d4 100644 --- a/tests/php/ORM/DataObjectLazyLoadingTest.php +++ b/tests/php/ORM/DataObjectLazyLoadingTest.php @@ -150,6 +150,21 @@ class DataObjectLazyLoadingTest extends SapphireTest ); } + public function testDBObjectLazyLoadedFields() + { + $subteam1 = $this->objFromFixture(SubTeam::class, 'subteam1'); + $teams = DataObject::get(Team::class); // query parent class + $subteam1Lazy = $teams->find('ID', $subteam1->ID); + + $subteam1DO = $subteam1->dbObject('SubclassDatabaseField'); + $subteam1LazyDO = $subteam1Lazy->dbObject('SubclassDatabaseField'); + + $this->assertEquals( + $subteam1DO->getValue(), + $subteam1LazyDO->getValue() + ); + } + public function testLazyLoadedFieldsSetField() { $subteam1 = $this->objFromFixture(SubTeam::class, 'subteam1');