diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php index 1adaf9f1460..e461c6abb70 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -1491,7 +1491,7 @@ protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$ma $specification = $schema->fieldSpec( $class, $fieldName, - DataObjectSchema::DB_ONLY | DataObjectSchema::UNINHERITED + DataObjectSchema::UNINHERITED ); if (!$specification) { continue; diff --git a/tests/php/ORM/DBCompositeTest.php b/tests/php/ORM/DBCompositeTest.php index 68acb97c9ac..c4fe99ecc1f 100644 --- a/tests/php/ORM/DBCompositeTest.php +++ b/tests/php/ORM/DBCompositeTest.php @@ -53,7 +53,8 @@ public function testCompositeFieldMetaDataFunctions() $this->assertEquals( [ 'MyMoney' => 'Money', - 'OverriddenMoney' => 'Money' + 'OverriddenMoney' => 'Money', + 'DoubleMoney' => DBCompositeTest\DBDoubleMoney::class ], $schema->compositeFields(DBCompositeTest\TestObject::class) ); @@ -68,6 +69,7 @@ public function testCompositeFieldMetaDataFunctions() 'MyMoney' => 'Money', 'OtherMoney' => 'Money', 'OverriddenMoney' => 'Money', + 'DoubleMoney' => DBCompositeTest\DBDoubleMoney::class ], $schema->compositeFields(DBCompositeTest\SubclassedDBFieldObject::class) ); @@ -111,4 +113,22 @@ public function testInheritedTables() $this->assertEquals('DBCompositeTest_SubclassedDBFieldObject', $object2->dbObject('OtherMoney')->getTable()); $this->assertEquals('DBCompositeTest_SubclassedDBFieldObject', $object2->dbObject('OverriddenMoney')->getTable()); } + + public function testWriteToManipuationIsCalledWhenWritingDataObject() + { + $obj = DBCompositeTest\TestObject::create(); + $obj->DoubleMoney = ['Amount' => 10, 'Currency' => 'CAD']; + $moneyField = $obj->dbObject('DoubleMoney'); + $this->assertEquals(10, $moneyField->getAmount()); + + $obj->write(); + + // Custom money class should double the amount before writing + $this->assertEquals(20, $moneyField->getAmount()); + + // Note: these would fail since dbObject will return a new instance + // of the DoubleMoney field based on the initial values + // $this->assertSame($moneyField, $obj->dbObject('DoubleMoney')); + // $this->assertEquals(20, $obj->dbObject('DoubleMoney')->getAmount()); + } } diff --git a/tests/php/ORM/DBCompositeTest/DBDoubleMoney.php b/tests/php/ORM/DBCompositeTest/DBDoubleMoney.php new file mode 100644 index 00000000000..1952e3cec9f --- /dev/null +++ b/tests/php/ORM/DBCompositeTest/DBDoubleMoney.php @@ -0,0 +1,17 @@ +setAmount($this->getAmount() * 2); + + parent::writeToManipulation($manipulation); + } +} diff --git a/tests/php/ORM/DBCompositeTest/TestObject.php b/tests/php/ORM/DBCompositeTest/TestObject.php index cfc74b2d963..10658c6933e 100644 --- a/tests/php/ORM/DBCompositeTest/TestObject.php +++ b/tests/php/ORM/DBCompositeTest/TestObject.php @@ -12,6 +12,7 @@ class TestObject extends DataObject implements TestOnly private static $db = [ 'Title' => 'Text', 'MyMoney' => 'Money', - 'OverriddenMoney' => 'Money' + 'OverriddenMoney' => 'Money', + 'DoubleMoney' => DBDoubleMoney::class, ]; }