Skip to content
This repository
Browse code

BUGFIX Allowing to save ListboxField and CheckboxSetField into custom…

… relations (which don't return TRUE for has_many() or many_many())
  • Loading branch information...
commit b43bdf3781a591c029936107377b4a1eb3880967 1 parent 79d420f
Ingo Schommer authored March 06, 2012
7  forms/CheckboxSetField.php
@@ -181,15 +181,16 @@ function setValue($value, $obj = null) {
181 181
 	 * @param DataObject $record The record to save into
182 182
 	 */
183 183
 	function saveInto(DataObject $record) {
184  
-		$fieldname = $this->name ;
185  
-		if($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
  184
+		$fieldname = $this->name;
  185
+		$relation = ($fieldname && $record && $record->hasMethod($fieldname)) ? $record->$fieldname() : null;
  186
+		if($fieldname && $record && $relation && $relation instanceof RelationList) {
186 187
 			$idList = array();
187 188
 			if($this->value) foreach($this->value as $id => $bool) {
188 189
 			   if($bool) {
189 190
 					$idList[] = $id;
190 191
 				}
191 192
 			}
192  
-			$record->$fieldname()->setByIDList($idList);
  193
+			$relation->setByIDList($idList);
193 194
 		} elseif($fieldname && $record) {
194 195
 			if($this->value) {
195 196
 				$this->value = str_replace(',', '{comma}', $this->value);
5  forms/ListboxField.php
@@ -172,10 +172,11 @@ function dataValue() {
172 172
 	function saveInto(DataObject $record) {
173 173
 		if($this->multiple) {
174 174
 			$fieldname = $this->name;
175  
-			if($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
  175
+			$relation = ($fieldname && $record && $record->hasMethod($fieldname)) ? $record->$fieldname() : null;
  176
+			if($fieldname && $record && $relation && $relation instanceof RelationList) {
176 177
 				$idList = (is_array($this->value)) ? array_values($this->value) : array();
177 178
 				if(!$record->ID) $record->write(); // record needs to have an ID in order to set relationships
178  
-				$record->$fieldname()->setByIDList($idList);
  179
+				$relation->setByIDList($idList);
179 180
 			} elseif($fieldname && $record) {
180 181
 				if($this->value) {
181 182
 					$this->value = str_replace(',', '{comma}', $this->value);

0 notes on commit b43bdf3

Please sign in to comment.
Something went wrong with that request. Please try again.