Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBComposite::writeToManipulation() is never called #10913

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 34 additions & 1 deletion tests/php/ORM/DBCompositeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function testCompositeFieldMetaDataFunctions()
$this->assertEquals(
[
'MyMoney' => 'Money',
'OverriddenMoney' => 'Money'
'OverriddenMoney' => 'Money',
'DoubleMoney' => DBCompositeTest\DBDoubleMoney::class
],
$schema->compositeFields(DBCompositeTest\TestObject::class)
);
Expand All @@ -68,6 +69,7 @@ public function testCompositeFieldMetaDataFunctions()
'MyMoney' => 'Money',
'OtherMoney' => 'Money',
'OverriddenMoney' => 'Money',
'DoubleMoney' => DBCompositeTest\DBDoubleMoney::class
],
$schema->compositeFields(DBCompositeTest\SubclassedDBFieldObject::class)
);
Expand Down Expand Up @@ -111,4 +113,35 @@ public function testInheritedTables()
$this->assertEquals('DBCompositeTest_SubclassedDBFieldObject', $object2->dbObject('OtherMoney')->getTable());
$this->assertEquals('DBCompositeTest_SubclassedDBFieldObject', $object2->dbObject('OverriddenMoney')->getTable());
}

/* Uncomment this for SS5
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';
}
*/
lekoala marked this conversation as resolved.
Show resolved Hide resolved

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());
}
}
17 changes: 17 additions & 0 deletions tests/php/ORM/DBCompositeTest/DBDoubleMoney.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 2 additions & 1 deletion tests/php/ORM/DBCompositeTest/TestObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TestObject extends DataObject implements TestOnly
private static $db = [
'Title' => 'Text',
'MyMoney' => 'Money',
'OverriddenMoney' => 'Money'
'OverriddenMoney' => 'Money',
'DoubleMoney' => DBDoubleMoney::class,
];
}