mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
BUGFIX #3959: Fixed auto-setting has many relations on CTF - works mostly like the many-many relation auto setting.
MINOR Added test cases for auto-setting has-many relations git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@75743 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
faeed904dc
commit
78d3689729
@ -505,19 +505,29 @@ JS;
|
||||
$manyManyRelationName = null;
|
||||
$manyManyComponentSet = null;
|
||||
|
||||
$hasManyRelations = $parentClass->has_many();
|
||||
$hasManyRelationName = null;
|
||||
$hasManyComponentSet = null;
|
||||
|
||||
if($manyManyRelations) foreach($manyManyRelations as $relation => $class) {
|
||||
if($class == $this->sourceClass()) {
|
||||
$manyManyRelationName = $relation;
|
||||
}
|
||||
}
|
||||
|
||||
if($hasManyRelations) foreach($hasManyRelations as $relation => $class) {
|
||||
if($class == $this->sourceClass()) {
|
||||
$hasManyRelationName = $relation;
|
||||
}
|
||||
}
|
||||
|
||||
// Add the relation value to related records
|
||||
if(!$childData->ID && $this->getParentClass()) {
|
||||
// make sure the relation-link is existing, even if we just add the sourceClass and didn't save it
|
||||
$parentIDName = $this->getParentIdName( $this->getParentClass(), $this->sourceClass() );
|
||||
$parentIDName = $this->getParentIdName($this->getParentClass(), $this->sourceClass());
|
||||
$childData->$parentIDName = $this->sourceID();
|
||||
}
|
||||
|
||||
|
||||
$detailFields = $this->getCustomFieldsFor($childData);
|
||||
|
||||
// Loading of extra field values for editing an existing record
|
||||
@ -530,6 +540,10 @@ JS;
|
||||
$field->setValue($fieldValue);
|
||||
}
|
||||
}
|
||||
|
||||
if($hasManyRelationName && $childData->ID) {
|
||||
$hasManyComponentSet = $parentClass->getComponents($hasManyRelationName);
|
||||
}
|
||||
|
||||
// the ID field confuses the Controller-logic in finding the right view for ReferencedField
|
||||
$detailFields->removeByName('ID');
|
||||
@ -538,22 +552,28 @@ JS;
|
||||
if($childData->ID) {
|
||||
$detailFields->push(new HiddenField('ctf[childID]', '', $childData->ID));
|
||||
}
|
||||
|
||||
|
||||
// add a namespaced ID instead thats "converted" by saveComplexTableField()
|
||||
$detailFields->push(new HiddenField('ctf[ClassName]', '', $this->sourceClass()));
|
||||
|
||||
if($this->getParentClass()) {
|
||||
$detailFields->push(new HiddenField('ctf[parentClass]', '', $this->getParentClass()));
|
||||
|
||||
if($manyManyRelationName && $this->relationAutoSetting) {
|
||||
$detailFields->push(new HiddenField('ctf[manyManyRelation]', '', $manyManyRelationName));
|
||||
$detailFields->push(new HiddenField('ctf[parentClass]', '', $this->getParentClass()));
|
||||
}
|
||||
|
||||
if($hasManyRelationName && $this->relationAutoSetting) {
|
||||
$detailFields->push(new HiddenField('ctf[hasManyRelation]', '', $hasManyRelationName));
|
||||
}
|
||||
|
||||
if($manyManyRelationName || $hasManyRelationName) {
|
||||
$detailFields->push(new HiddenField('ctf[sourceID]', '', $this->sourceID()));
|
||||
}
|
||||
|
||||
$parentIdName = $this->getParentIdName($this->getParentClass(), $this->sourceClass());
|
||||
|
||||
if($parentIdName) {
|
||||
// add relational fields
|
||||
$detailFields->push(new HiddenField('ctf[parentClass]', '', $this->getParentClass()));
|
||||
|
||||
if($this->relationAutoSetting) {
|
||||
// Hack for model admin: model admin will have included a dropdown for the relation itself
|
||||
$detailFields->removeByName($parentIdName);
|
||||
@ -634,7 +654,6 @@ JS;
|
||||
|
||||
// Save the many many relationship if it's available
|
||||
if(isset($data['ctf']['manyManyRelation'])) {
|
||||
|
||||
$parentRecord = DataObject::get_by_id($data['ctf']['parentClass'], (int) $data['ctf']['sourceID']);
|
||||
$relationName = $data['ctf']['manyManyRelation'];
|
||||
$extraFields = array();
|
||||
@ -649,6 +668,14 @@ JS;
|
||||
$componentSet->add($childData, $extraFields);
|
||||
}
|
||||
|
||||
if(isset($data['ctf']['hasManyRelation'])) {
|
||||
$parentRecord = DataObject::get_by_id($data['ctf']['parentClass'], (int) $data['ctf']['sourceID']);
|
||||
$relationName = $data['ctf']['hasManyRelation'];
|
||||
|
||||
$componentSet = $parentRecord->getComponents($relationName);
|
||||
$componentSet->add($childData);
|
||||
}
|
||||
|
||||
$referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
|
||||
|
||||
$closeLink = sprintf(
|
||||
@ -820,8 +847,10 @@ class ComplexTableField_ItemRequest extends RequestHandler {
|
||||
$componentSet->add($dataObject, $extraFields);
|
||||
}
|
||||
|
||||
$referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
|
||||
|
||||
$closeLink = sprintf(
|
||||
'<small><a href="' . $_SERVER['HTTP_REFERER'] . '" onclick="javascript:window.top.GB_hide(); return false;">(%s)</a></small>',
|
||||
'<small><a href="' . $referrer . '" onclick="javascript:window.top.GB_hide(); return false;">(%s)</a></small>',
|
||||
_t('ComplexTableField.CLOSEPOPUP', 'Close Popup')
|
||||
);
|
||||
$message = sprintf(
|
||||
|
@ -27,11 +27,11 @@ class ComplexTableFieldTest extends FunctionalTest {
|
||||
parent::setUp();
|
||||
|
||||
$this->controller = new ComplexTableFieldTest_Controller();
|
||||
$this->form = $this->controller->Form();
|
||||
$this->manyManyForm = $this->controller->ManyManyForm();
|
||||
}
|
||||
|
||||
function testCorrectNumberOfRowsInTable() {
|
||||
$field = $this->form->dataFieldByName('Players');
|
||||
$field = $this->manyManyForm->dataFieldByName('Players');
|
||||
$parser = new CSSContentParser($field->FieldHolder());
|
||||
|
||||
/* There are 2 players (rows) in the table */
|
||||
@ -42,7 +42,7 @@ class ComplexTableFieldTest extends FunctionalTest {
|
||||
}
|
||||
|
||||
function testDetailFormDisplaysWithCorrectFields() {
|
||||
$field = $this->form->dataFieldByName('Players');
|
||||
$field = $this->manyManyForm->dataFieldByName('Players');
|
||||
$detailForm = $field->add();
|
||||
$parser = new CSSContentParser($detailForm);
|
||||
|
||||
@ -56,7 +56,7 @@ class ComplexTableFieldTest extends FunctionalTest {
|
||||
function testAddingNewPlayerWithExtraData() {
|
||||
$team = DataObject::get_one('ComplexTableFieldTest_Team', "Name = 'The Awesome People'");
|
||||
|
||||
$this->post('ComplexTableFieldTest_Controller/Form/field/Players/AddForm', array(
|
||||
$this->post('ComplexTableFieldTest_Controller/ManyManyForm/field/Players/AddForm', array(
|
||||
'Name' => 'Bobby Joe',
|
||||
'ctf' => array(
|
||||
'extraFields' => array(
|
||||
@ -85,7 +85,42 @@ class ComplexTableFieldTest extends FunctionalTest {
|
||||
$extraFields = $teams->getExtraData('Teams', $team->ID);
|
||||
$this->assertEquals($extraFields['Role'], 'Goalie', 'The extra fields have the correct value');
|
||||
}
|
||||
|
||||
function testAddingHasManyData() {
|
||||
$team = DataObject::get_one('ComplexTableFieldTest_Team', "Name = 'The Awesome People'");
|
||||
|
||||
$this->post('ComplexTableFieldTest_Controller/HasManyForm/field/Sponsors/AddForm', array(
|
||||
'Name' => 'Jim Beam',
|
||||
'ctf' => array(
|
||||
'ClassName' => 'ComplexTableFieldTest_Sponsor',
|
||||
'hasManyRelation' => 'Sponsors',
|
||||
'parentClass' => 'ComplexTableFieldTest_Team',
|
||||
'sourceID' => $team->ID
|
||||
)
|
||||
));
|
||||
|
||||
/* Retrieve the new sponsor record we created */
|
||||
$newSponsor = DataObject::get_one('ComplexTableFieldTest_Sponsor', "Name = 'Jim Beam'");
|
||||
|
||||
/* A new ComplexTableFieldTest_Sponsor record was created, Name = "Jim Beam" */
|
||||
$this->assertNotNull($newSponsor, 'A new ComplexTableFieldTest_Sponsor record was created, Name = "Jim Beam"');
|
||||
|
||||
/* Get the has-one related Team to the new sponsor that were automatically linked by CTF */
|
||||
$teamID = $newSponsor->TeamID;
|
||||
|
||||
/* Automatic many-many relation was set correctly on the new player */
|
||||
$this->assertTrue($teamID > 0, 'Automatic has-many/has-one relation was set correctly on the sponsor');
|
||||
|
||||
/* The other side of the relation works as well */
|
||||
$team = DataObject::get_by_id('ComplexTableFieldTest_Team', $teamID);
|
||||
|
||||
/* Let's get the Sponsors component */
|
||||
$sponsor = $team->getComponents('Sponsors')->First();
|
||||
|
||||
/* The sponsor is the same as the one we added */
|
||||
$this->assertEquals($newSponsor->ID, $sponsor->ID, 'The sponsor is the same as the one we added');
|
||||
}
|
||||
|
||||
}
|
||||
class ComplexTableFieldTest_Controller extends Controller {
|
||||
|
||||
@ -93,7 +128,7 @@ class ComplexTableFieldTest_Controller extends Controller {
|
||||
return "ComplexTableFieldTest_Controller/$action";
|
||||
}
|
||||
|
||||
function Form() {
|
||||
function ManyManyForm() {
|
||||
$team = DataObject::get_one('ComplexTableFieldTest_Team', "Name = 'The Awesome People'");
|
||||
|
||||
$playersField = new ComplexTableField(
|
||||
@ -108,7 +143,7 @@ class ComplexTableFieldTest_Controller extends Controller {
|
||||
|
||||
$form = new Form(
|
||||
$this,
|
||||
'Form',
|
||||
'ManyManyForm',
|
||||
new FieldSet(
|
||||
new HiddenField('ID', '', $team->ID),
|
||||
$playersField
|
||||
@ -122,6 +157,36 @@ class ComplexTableFieldTest_Controller extends Controller {
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
function HasManyForm() {
|
||||
$team = DataObject::get_one('ComplexTableFieldTest_Team', "Name = 'The Awesome People'");
|
||||
|
||||
$sponsorsField = new ComplexTableField(
|
||||
$this,
|
||||
'Sponsors',
|
||||
'ComplexTableFieldTest_Sponsor',
|
||||
ComplexTableFieldTest_Sponsor::$summary_fields,
|
||||
'getCMSFields'
|
||||
);
|
||||
|
||||
$sponsorsField->setParentClass('ComplexTableFieldTest_Team');
|
||||
|
||||
$form = new Form(
|
||||
$this,
|
||||
'HasManyForm',
|
||||
new FieldSet(
|
||||
new HiddenField('ID', '', $team->ID),
|
||||
$sponsorsField
|
||||
),
|
||||
new FieldSet(
|
||||
new FormAction('doSubmit', 'Submit')
|
||||
)
|
||||
);
|
||||
|
||||
$form->disableSecurityToken();
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
}
|
||||
class ComplexTableFieldTest_Player extends DataObject implements TestOnly {
|
||||
@ -151,5 +216,20 @@ class ComplexTableFieldTest_Team extends DataObject implements TestOnly {
|
||||
'Players' => 'ComplexTableFieldTest_Player'
|
||||
);
|
||||
|
||||
public static $has_many = array(
|
||||
'Sponsors' => 'ComplexTableFieldTest_Sponsor'
|
||||
);
|
||||
|
||||
}
|
||||
class ComplexTableFieldTest_Sponsor extends DataObject implements TestOnly {
|
||||
|
||||
public static $db = array(
|
||||
'Name' => 'Varchar(100)'
|
||||
);
|
||||
|
||||
public static $has_one = array(
|
||||
'Team' => 'ComplexTableFieldTest_Team'
|
||||
);
|
||||
|
||||
}
|
||||
?>
|
@ -8,3 +8,8 @@ ComplexTableFieldTest_Team:
|
||||
Name: The Awesome People
|
||||
t2:
|
||||
Name: Incredible Four
|
||||
ComplexTableFieldTest_Sponsor:
|
||||
s1:
|
||||
Name: Coca Cola
|
||||
s2:
|
||||
Name: Pepsi
|
Loading…
x
Reference in New Issue
Block a user