public function saveInto(DataObjectInterface $record) { $isNew = !$record->exists(); parent::saveInto($record); // if we're dealing with an unsaved record, we have to rebuild the relation list // with the proper meny_many_extraFields attributes (eg. the sort order) if ($isNew) { // we have to grab the raw post data as the data is in the right order there. // this is kind of a hack, but we simply lack the information about the client-side sorting otherwise if (isset($_POST[$this->name]) && isset($_POST[$this->name]['Files']) && is_array($_POST[$this->name]['Files'])) { $idList = $_POST[$this->name]['Files']; } else { // take the ItemIDs as a fallback $idList = $this->getItemIDs(); } $sortColumn = $this->getSortColumn(); $relationName = $this->getName(); if ($relationName && $record->many_many($relationName) !== null && ($list = $record->{$relationName}())) { $arrayList = $list->toArray(); foreach ($arrayList as $item) { $list->remove($item); $list->add($item, array($sortColumn => array_search($item->ID, $idList) + 1)); } } } }
public function saveInto(DataObjectInterface $record) { $isNew = !$record->exists(); parent::saveInto($record); // if we're dealing with an unsaved record, we have to rebuild the relation list // with the proper many_many_extraFields attributes (eg. the sort order) if ($isNew) { // we have to grab the raw post data as the data is in the right order there. // this is kind of a hack, but we simply lack the information about the client-side sorting otherwise if (isset($_POST[$this->name]) && isset($_POST[$this->name]['Files']) && is_array($_POST[$this->name]['Files'])) { $idList = $_POST[$this->name]['Files']; } else { // take the ItemIDs as a fallback $idList = $this->getItemIDs(); } // Get general info about this relation (if possible) $sortColumn = $this->getSortColumn(); $relationName = $this->getName(); if ($relationName) { // Get relation type. $isManyMany = $record->many_many($relationName) !== null; // Using "null" here since we know it will properly return null if there is no many_many component by this name. $isHasMany = (bool) $record->has_many($relationName); // TODO: Return to more strict check of === null here when SS framework issue is mainstream: https://github.com/silverstripe/silverstripe-framework/pull/4130 if ($isManyMany || $isHasMany) { // Get list from that relation and begin to manipulate it. $list = $record->{$relationName}(); $arrayList = $list->toArray(); foreach ($arrayList as $item) { // Get the specified sort order for this item (offset from posted 0 index). $sortOrder = array_search($item->ID, $idList) + 1; // The method by which we update this relation varies depending on the relationship type... if ($isManyMany) { // Alter data in the pivot table. $list->remove($item); $list->add($item, array($sortColumn => $sortOrder)); } elseif ($isHasMany) { // Just update information in the sort field directly on the item itself. $item->{$sortColumn} = $sortOrder; $item->write(); } } } } } }
/** * Called before the dataobject record is saved. * @param DataObjectInterface $record The record. * @return [type] [description] */ public function saveInto(DataObjectInterface $record) { // On the dataobject record set the lat, long, zoom fields (names specified by the dev at // time of construction) to the values of the lat long and zoom child fields of this class. $record->setCastedField($this->latFieldName, $this->latField->dataValue()); $record->setCastedField($this->lngFieldName, $this->lngField->dataValue()); $record->setCastedField($this->zoomFieldName, $this->zoomField->dataValue()); // Do parent stuff as normal. return parent::saveInto($record); }