ENHANCEMENT Added TableField?->autoRelationSetting to allow for automatic saving into relationships (similiar to ComplexTableField? implementation) (merged from branches/2.3-nzct)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@82060 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-07-16 23:49:32 +00:00
parent cca0d327e7
commit 8a20d5c1ad
2 changed files with 133 additions and 0 deletions

View File

@ -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;
}
}
/**

View File

@ -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'
);
}