mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT Allow getting extra field data for a many-to-many relation by calling getExtraData() on a ComponentSet and passing the component name, and child ID
ENHANCEMENT Hooked up setting field values for many_many_extraFields into ComplexTableField. See r71613 for the original many-to-many auto setting addition BUGFIX When editing an existing record, many-to-many auto setting wasn't being done because saveComplexTableField() is called on ComplexTableField_ItemRequest git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@71635 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
bbe3bc0535
commit
df36ae07e8
@ -60,6 +60,35 @@ class ComponentSet extends DataObjectSet {
|
||||
$this->joinField = $joinField;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the extra field data for a single row of the relationship
|
||||
* join table, given the known child ID.
|
||||
*
|
||||
* @param string $componentName The name of the component
|
||||
* @param int $childID The ID of the child for the relationship
|
||||
* @return array Map of fieldName => fieldValue
|
||||
*/
|
||||
function getExtraData($componentName, $childID) {
|
||||
$ownerObj = $this->ownerObj;
|
||||
$parentField = $this->ownerClass . 'ID';
|
||||
$childField = ($this->childClass == $this->ownerClass) ? 'ChildID' : ($this->childClass . 'ID');
|
||||
$result = array();
|
||||
|
||||
if(!$componentName) return false;
|
||||
|
||||
// @todo Optimize into a single query instead of one per extra field
|
||||
$extraFields = $ownerObj->many_many_extraFields($componentName);
|
||||
if($extraFields) {
|
||||
foreach($extraFields as $fieldName => $dbFieldSpec) {
|
||||
$query = DB::query("SELECT \"$fieldName\" FROM \"$this->tableName\" WHERE \"$parentField\" = {$this->ownerObj->ID} AND \"$childField\" = {$childID}");
|
||||
$value = $query->value();
|
||||
$result[$fieldName] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of all the IDs in this component set, where the keys are the same as the
|
||||
* values.
|
||||
@ -92,7 +121,7 @@ class ComponentSet extends DataObjectSet {
|
||||
user_error("ComponentSet::add() \$this->childClass not set", E_USER_ERROR);
|
||||
}
|
||||
|
||||
$item = DataObject::get_by_id($this->childClass, $item);
|
||||
$item = DataObject::get_by_id($this->childClass, $item);
|
||||
if(!$item) return;
|
||||
}
|
||||
|
||||
|
@ -488,9 +488,10 @@ JS;
|
||||
|
||||
function getFieldsFor($childData) {
|
||||
// See if our parent class has any many_many relations by this source class
|
||||
$SNG_parentClass = singleton($this->getParentClass());
|
||||
$manyManyRelations = $SNG_parentClass->many_many();
|
||||
$parentClass = DataObject::get_by_id($this->getParentClass(), $this->sourceID());
|
||||
$manyManyRelations = $parentClass->many_many();
|
||||
$manyManyRelationName = null;
|
||||
$manyManyComponentSet = null;
|
||||
|
||||
if($manyManyRelations) foreach($manyManyRelations as $relation => $class) {
|
||||
if($class == $this->sourceClass()) {
|
||||
@ -507,6 +508,16 @@ JS;
|
||||
|
||||
$detailFields = $this->getCustomFieldsFor($childData);
|
||||
|
||||
// Loading of extra field values for editing an existing record
|
||||
if($manyManyRelationName && $childData->ID) {
|
||||
$manyManyComponentSet = $parentClass->getManyManyComponents($manyManyRelationName);
|
||||
$extraData = $manyManyComponentSet->getExtraData($manyManyRelationName, $childData->ID);
|
||||
if($extraData) foreach($extraData as $fieldName => $fieldValue) {
|
||||
$field = $detailFields->dataFieldByName('ctf[extraFields][' . $fieldName . ']');
|
||||
$field->setValue($fieldValue);
|
||||
}
|
||||
}
|
||||
|
||||
// the ID field confuses the Controller-logic in finding the right view for ReferencedField
|
||||
$detailFields->removeByName('ID');
|
||||
|
||||
@ -605,7 +616,7 @@ JS;
|
||||
$childData = new $className();
|
||||
$form->saveInto($childData);
|
||||
$childData->write();
|
||||
|
||||
|
||||
// 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']);
|
||||
@ -770,10 +781,25 @@ class ComplexTableField_ItemRequest extends RequestHandler {
|
||||
*/
|
||||
function saveComplexTableField($data, $form, $request) {
|
||||
$dataObject = $this->dataObj();
|
||||
|
||||
$form->saveInto($dataObject);
|
||||
$dataObject->write();
|
||||
|
||||
// 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();
|
||||
|
||||
if(isset($data['ctf']['extraFields'])) {
|
||||
foreach($data['ctf']['extraFields'] as $field => $value) {
|
||||
$extraFields[$field] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$componentSet = $parentRecord->getManyManyComponents($relationName);
|
||||
$componentSet->add($dataObject, $extraFields);
|
||||
}
|
||||
|
||||
$closeLink = sprintf(
|
||||
'<small><a href="' . $_SERVER['HTTP_REFERER'] . '" onclick="javascript:window.top.GB_hide(); return false;">(%s)</a></small>',
|
||||
_t('ComplexTableField.CLOSEPOPUP', 'Close Popup')
|
||||
|
Loading…
Reference in New Issue
Block a user