Permalink
Browse files

BUGFIX Allowing to save ListboxField and CheckboxSetField into custom…

… relations (which don't return TRUE for has_many() or many_many())
  • Loading branch information...
1 parent 79d420f commit b43bdf3781a591c029936107377b4a1eb3880967 @chillu chillu committed Mar 6, 2012
Showing with 7 additions and 5 deletions.
  1. +4 −3 forms/CheckboxSetField.php
  2. +3 −2 forms/ListboxField.php
@@ -181,15 +181,16 @@ function setValue($value, $obj = null) {
* @param DataObject $record The record to save into
*/
function saveInto(DataObject $record) {
- $fieldname = $this->name ;
- if($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
+ $fieldname = $this->name;
+ $relation = ($fieldname && $record && $record->hasMethod($fieldname)) ? $record->$fieldname() : null;
+ if($fieldname && $record && $relation && $relation instanceof RelationList) {
$idList = array();
if($this->value) foreach($this->value as $id => $bool) {
if($bool) {
$idList[] = $id;
}
}
- $record->$fieldname()->setByIDList($idList);
+ $relation->setByIDList($idList);
} elseif($fieldname && $record) {
if($this->value) {
$this->value = str_replace(',', '{comma}', $this->value);
View
@@ -172,10 +172,11 @@ function dataValue() {
function saveInto(DataObject $record) {
if($this->multiple) {
$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();
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) {
if($this->value) {
$this->value = str_replace(',', '{comma}', $this->value);

0 comments on commit b43bdf3

Please sign in to comment.