BUGFIX Allowing to save ListboxField and CheckboxSetField into custom relations (which don't return TRUE for has_many() or many_many())

This commit is contained in:
Ingo Schommer 2012-03-06 23:47:09 +01:00
parent 79d420f941
commit b43bdf3781
2 changed files with 7 additions and 5 deletions

View File

@ -181,15 +181,16 @@ class CheckboxSetField extends OptionsetField {
* @param DataObject $record The record to save into * @param DataObject $record The record to save into
*/ */
function saveInto(DataObject $record) { function saveInto(DataObject $record) {
$fieldname = $this->name ; $fieldname = $this->name;
if($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) { $relation = ($fieldname && $record && $record->hasMethod($fieldname)) ? $record->$fieldname() : null;
if($fieldname && $record && $relation && $relation instanceof RelationList) {
$idList = array(); $idList = array();
if($this->value) foreach($this->value as $id => $bool) { if($this->value) foreach($this->value as $id => $bool) {
if($bool) { if($bool) {
$idList[] = $id; $idList[] = $id;
} }
} }
$record->$fieldname()->setByIDList($idList); $relation->setByIDList($idList);
} elseif($fieldname && $record) { } elseif($fieldname && $record) {
if($this->value) { if($this->value) {
$this->value = str_replace(',', '{comma}', $this->value); $this->value = str_replace(',', '{comma}', $this->value);

View File

@ -172,10 +172,11 @@ class ListboxField extends DropdownField {
function saveInto(DataObject $record) { function saveInto(DataObject $record) {
if($this->multiple) { if($this->multiple) {
$fieldname = $this->name; $fieldname = $this->name;
if($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) { $relation = ($fieldname && $record && $record->hasMethod($fieldname)) ? $record->$fieldname() : null;
if($fieldname && $record && $relation && $relation instanceof RelationList) {
$idList = (is_array($this->value)) ? array_values($this->value) : array(); $idList = (is_array($this->value)) ? array_values($this->value) : array();
if(!$record->ID) $record->write(); // record needs to have an ID in order to set relationships if(!$record->ID) $record->write(); // record needs to have an ID in order to set relationships
$record->$fieldname()->setByIDList($idList); $relation->setByIDList($idList);
} elseif($fieldname && $record) { } elseif($fieldname && $record) {
if($this->value) { if($this->value) {
$this->value = str_replace(',', '{comma}', $this->value); $this->value = str_replace(',', '{comma}', $this->value);