diff --git a/forms/TableField.php b/forms/TableField.php index 9115e1de5..357376587 100644 --- a/forms/TableField.php +++ b/forms/TableField.php @@ -98,6 +98,14 @@ class TableField extends TableListField { * @param boolean $showAddRow */ public $showAddRow = true; + + /** + * Automatically detect a has-one relationship + * in the popup (=child-class) and save the relation ID. + * + * @var boolean + */ + protected $relationAutoSetting = true; function __construct($name, $sourceClass, $fieldList = null, $fieldTypes, $filterField = null, $sourceFilter = null, $editExisting = true, $sourceSort = null, $sourceJoin = null) { @@ -281,7 +289,22 @@ class TableField extends TableListField { } else if(isset($newFields) && $newFields) { $savedObj = $this->saveData($newFields,false); } + + // Optionally save the newly created records into a relationship + // on $record. This assumes the name of this formfield instance + // is set to a relationship name on $record. + if($this->relationAutoSetting) { + $relationName = $this->Name(); + if($record->has_many($relationName) || $record->many_many($relationName)) { + if($savedObj) foreach($savedObj as $id => $status) { + $record->$relationName()->add($id); + } + } + } + + // Update the internal source items cache $items = $this->sourceItems(); + FormResponse::update_dom_id($this->id(), $this->FieldHolder()); } } @@ -613,6 +636,20 @@ JS; function setRequiredFields($fields) { $this->requiredFields = $fields; } + + /** + * @param boolean $value + */ + function setRelationAutoSetting($value) { + $this->relationAutoSetting = $value; + } + + /** + * @return boolean + */ + function getRelationAutoSetting() { + return $this->relationAutoSetting; + } } /** diff --git a/tests/forms/TableFieldTest.php b/tests/forms/TableFieldTest.php index 41be95071..94be2344c 100644 --- a/tests/forms/TableFieldTest.php +++ b/tests/forms/TableFieldTest.php @@ -3,6 +3,7 @@ class TableFieldTest extends SapphireTest { static $fixture_file = 'sapphire/tests/forms/TableFieldTest.yml'; + function testTableFieldSaving() { $group = $this->objFromFixture('Group','a'); @@ -70,6 +71,85 @@ class TableFieldTest extends SapphireTest { ), $permissions); } + + function testAutoRelationSettingOn() { + $tf = new TableField( + 'HasManyRelations', + 'TableFieldTest_HasManyRelation', + array( + 'Value' => 'Value' + ), + array( + 'Value' => 'TextField' + ) + ); + + // Test with auto relation setting + $form = new Form(new TableFieldTest_Controller(), "Form", new FieldSet($tf), new FieldSet()); + $tf->setValue(array( + 'new' => array( + 'Value' => array('one','two',) + ) + )); + $tf->setRelationAutoSetting(true); + $o = new TableFieldTest_Object(); + $o->write(); + $form->saveInto($o); + $this->assertEquals($o->HasManyRelations()->Count(), 2); + } + + function testAutoRelationSettingOff() { + $tf = new TableField( + 'HasManyRelations', + 'TableFieldTest_HasManyRelation', + array( + 'Value' => 'Value' + ), + array( + 'Value' => 'TextField' + ) + ); + + // Test with auto relation setting + $form = new Form(new TableFieldTest_Controller(), "Form", new FieldSet($tf), new FieldSet()); + $tf->setValue(array( + 'new' => array( + 'Value' => array('one','two',) + ) + )); + $tf->setRelationAutoSetting(false); + $o = new TableFieldTest_Object(); + $o->write(); + $form->saveInto($o); + $this->assertEquals($o->HasManyRelations()->Count(), 0); + } + + function testDataValue() { + $tf = new TableField( + 'TestTableField', + 'TestTableField', + array( + 'Currency' => 'Currency' + ), + array( + 'Currency' => 'CurrencyField' + ) + ); + $form = new Form(new TableFieldTest_Controller(), "Form", new FieldSet($tf), new FieldSet()); + $tf->setValue(array( + 'new' => array( + 'Currency' => array( + '$1,234.56', + '1234.57', + ) + ) + )); + $data = $form->getData(); + + // @todo Fix getData() + //$this->assertEquals($data['TestTableField']['new']['Currency'][0], 1234.56); + //$this->assertEquals($data['TestTableField']['new']['Currency'][1], 1234.57); + } } /** @@ -79,4 +159,20 @@ class TableFieldTest_Controller extends Controller { function Link() { return 'TableFieldTest/'; } +} + +class TableFieldTest_Object extends DataObject implements TestOnly { + static $has_many = array( + "HasManyRelations" => 'TableFieldTest_HasManyRelation' + ); +} + +class TableFieldTest_HasManyRelation extends DataObject implements TestOnly { + static $db = array( + 'Value' => 'Text', + ); + + static $has_one = array( + 'HasOneRelation' => 'TableFieldTest_Object' + ); } \ No newline at end of file