DBComposite::writeToManipulation() is never called

This commit is contained in:
Thomas 2023-08-10 10:37:21 +02:00 committed by Guy Sartorelli
parent 597d97bf0a
commit d621d00eea
No known key found for this signature in database
GPG Key ID: F313E3B9504D496A
4 changed files with 52 additions and 3 deletions

View File

@ -1491,7 +1491,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$specification = $schema->fieldSpec(
$class,
$fieldName,
DataObjectSchema::DB_ONLY | DataObjectSchema::UNINHERITED
DataObjectSchema::UNINHERITED
);
if (!$specification) {
continue;

View File

@ -53,7 +53,8 @@ class DBCompositeTest extends SapphireTest
$this->assertEquals(
[
'MyMoney' => 'Money',
'OverriddenMoney' => 'Money'
'OverriddenMoney' => 'Money',
'DoubleMoney' => DBCompositeTest\DBDoubleMoney::class
],
$schema->compositeFields(DBCompositeTest\TestObject::class)
);
@ -68,6 +69,7 @@ class DBCompositeTest extends SapphireTest
'MyMoney' => 'Money',
'OtherMoney' => 'Money',
'OverriddenMoney' => 'Money',
'DoubleMoney' => DBCompositeTest\DBDoubleMoney::class
],
$schema->compositeFields(DBCompositeTest\SubclassedDBFieldObject::class)
);
@ -111,4 +113,33 @@ class DBCompositeTest extends SapphireTest
$this->assertEquals('DBCompositeTest_SubclassedDBFieldObject', $object2->dbObject('OtherMoney')->getTable());
$this->assertEquals('DBCompositeTest_SubclassedDBFieldObject', $object2->dbObject('OverriddenMoney')->getTable());
}
public function testSetFieldDynamicPropertyException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(implode(' ', [
'Field abc does not exist.',
'If this was accessed via a dynamic property then call setDynamicData() instead.'
]));
$object = new DBCompositeTest\TestObject();
$object->MyMoney->abc = 'def';
}
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());
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace SilverStripe\ORM\Tests\DBCompositeTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\FieldType\DBMoney;
class DBDoubleMoney extends DBMoney implements TestOnly
{
public function writeToManipulation(&$manipulation)
{
// Duplicate the amount before writing
$this->setAmount($this->getAmount() * 2);
parent::writeToManipulation($manipulation);
}
}

View File

@ -12,6 +12,7 @@ class TestObject extends DataObject implements TestOnly
private static $db = [
'Title' => 'Text',
'MyMoney' => 'Money',
'OverriddenMoney' => 'Money'
'OverriddenMoney' => 'Money',
'DoubleMoney' => DBDoubleMoney::class,
];
}