mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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:
parent
cca0d327e7
commit
8a20d5c1ad
@ -99,6 +99,14 @@ class TableField extends TableListField {
|
|||||||
*/
|
*/
|
||||||
public $showAddRow = true;
|
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,
|
function __construct($name, $sourceClass, $fieldList = null, $fieldTypes, $filterField = null,
|
||||||
$sourceFilter = null, $editExisting = true, $sourceSort = null, $sourceJoin = null) {
|
$sourceFilter = null, $editExisting = true, $sourceSort = null, $sourceJoin = null) {
|
||||||
|
|
||||||
@ -281,7 +289,22 @@ class TableField extends TableListField {
|
|||||||
} else if(isset($newFields) && $newFields) {
|
} else if(isset($newFields) && $newFields) {
|
||||||
$savedObj = $this->saveData($newFields,false);
|
$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();
|
$items = $this->sourceItems();
|
||||||
|
|
||||||
FormResponse::update_dom_id($this->id(), $this->FieldHolder());
|
FormResponse::update_dom_id($this->id(), $this->FieldHolder());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -613,6 +636,20 @@ JS;
|
|||||||
function setRequiredFields($fields) {
|
function setRequiredFields($fields) {
|
||||||
$this->requiredFields = $fields;
|
$this->requiredFields = $fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param boolean $value
|
||||||
|
*/
|
||||||
|
function setRelationAutoSetting($value) {
|
||||||
|
$this->relationAutoSetting = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
function getRelationAutoSetting() {
|
||||||
|
return $this->relationAutoSetting;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
class TableFieldTest extends SapphireTest {
|
class TableFieldTest extends SapphireTest {
|
||||||
static $fixture_file = 'sapphire/tests/forms/TableFieldTest.yml';
|
static $fixture_file = 'sapphire/tests/forms/TableFieldTest.yml';
|
||||||
|
|
||||||
|
|
||||||
function testTableFieldSaving() {
|
function testTableFieldSaving() {
|
||||||
$group = $this->objFromFixture('Group','a');
|
$group = $this->objFromFixture('Group','a');
|
||||||
|
|
||||||
@ -70,6 +71,85 @@ class TableFieldTest extends SapphireTest {
|
|||||||
), $permissions);
|
), $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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -80,3 +160,19 @@ class TableFieldTest_Controller extends Controller {
|
|||||||
return 'TableFieldTest/';
|
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'
|
||||||
|
);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user