From df36ae07e87a1b6a689a1bb8707bb1e2af2ae972 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Wed, 11 Feb 2009 00:10:37 +0000 Subject: [PATCH] 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 --- core/model/ComponentSet.php | 31 ++++++++++++++++++++++++++++++- forms/ComplexTableField.php | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/core/model/ComponentSet.php b/core/model/ComponentSet.php index 5f804d5c9..a84b0c164 100755 --- a/core/model/ComponentSet.php +++ b/core/model/ComponentSet.php @@ -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; } diff --git a/forms/ComplexTableField.php b/forms/ComplexTableField.php index 216cc6879..c0fcf12e4 100755 --- a/forms/ComplexTableField.php +++ b/forms/ComplexTableField.php @@ -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( '(%s)', _t('ComplexTableField.CLOSEPOPUP', 'Close Popup')