/** * Loads the given form data into the underlying dataobject and relation * * @param array $data * @param Form $form * @throws ValidationException On error * @return DataObject Saved record */ protected function saveFormIntoRecord($data, $form) { $list = $this->gridField->getList(); // Check object matches the correct classname if (isset($data['ClassName']) && $data['ClassName'] != $this->record->ClassName) { $newClassName = $data['ClassName']; // The records originally saved attribute was overwritten by $form->saveInto($record) before. // This is necessary for newClassInstance() to work as expected, and trigger change detection // on the ClassName attribute $this->record->setClassName($this->record->ClassName); // Replace $record with a new instance $this->record = $this->record->newClassInstance($newClassName); } // Save form and any extra saved data into this dataobject $form->saveInto($this->record); $this->record->write(); $extraData = $this->getExtraSavedData($this->record, $list); $list->add($this->record, $extraData); return $this->record; }
/** * Remove an item from this relation. * Doesn't actually remove the item, it just clears the foreign key value. * * @param DataObject $item The DataObject to be removed * @todo Maybe we should delete the object instead? */ public function remove($item) { if (!$item instanceof $this->dataClass) { throw new InvalidArgumentException("HasManyList::remove() expecting a {$this->dataClass} object, or ID", E_USER_ERROR); } // Don't remove item which doesn't belong to this list $foreignID = $this->getForeignID(); $foreignKey = $this->getForeignKey(); if (empty($foreignID) || is_array($foreignID) && in_array($item->{$foreignKey}, $foreignID) || $foreignID == $item->{$foreignKey}) { $item->{$foreignKey} = null; $item->write(); } }
/** * Remove an item from this relation. * Doesn't actually remove the item, it just clears the foreign key value. * * @param DataObject $item The DataObject to be removed * @todo Maybe we should delete the object instead? */ public function remove($item) { if (!$item instanceof $this->dataClass) { throw new InvalidArgumentException("HasManyList::remove() expecting a {$this->dataClass} object, or ID", E_USER_ERROR); } // Don't remove item with unrelated class key $foreignClass = $this->getForeignClass(); $classNames = ClassInfo::subclassesFor($foreignClass); $classForeignKey = $this->classForeignKey; if (!in_array($item->{$classForeignKey}, $classNames)) { return; } // Don't remove item which doesn't belong to this list $foreignID = $this->getForeignID(); $foreignKey = $this->foreignKey; if (empty($foreignID) || is_array($foreignID) && in_array($item->{$foreignKey}, $foreignID) || $foreignID == $item->{$foreignKey}) { $item->{$foreignKey} = null; $item->{$classForeignKey} = null; $item->write(); } }