/** * Save the current value of this field into a DataObject. * If the field it is saving to is a has_many or many_many relationship, * it is saved by setByIDList(), otherwise it creates a comma separated * list for a standard DB text/varchar field. * * @param DataObject $record The record to save into */ public function saveInto(DataObjectInterface $record) { if ($this->multiple) { $fieldname = $this->name; $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 $relation = $fieldname && $record && $record->hasMethod($fieldname) ? $record->{$fieldname}() : null; } $relation->setByIDList($idList); } elseif ($fieldname && $record) { if ($this->value) { $this->value = str_replace(',', '{comma}', $this->value); $record->{$fieldname} = implode(",", $this->value); } else { $record->{$fieldname} = null; } } } else { parent::saveInto($record); } }
/** * {@inheritdoc} */ public function saveInto(DataObjectInterface $record) { parent::saveInto($record); $name = $this->getName(); $record->{$name} = join(',', $this->Value()); $record->write(); }
/** * {@inheritdoc} */ public function saveInto(DataObjectInterface $record) { parent::saveInto($record); $name = $this->getName(); $titleField = $this->getTitleField(); $source = $this->getSource(); $dataClass = $source->dataClass(); $values = $this->Value(); if (!$values) { $values = array(); } if (empty($record) || empty($source) || empty($titleField)) { return; } if (!$record->hasMethod($name)) { throw new Exception(sprintf("%s does not have a %s method", get_class($record), $name)); } $relation = $record->{$name}(); foreach ($values as $i => $value) { if (!is_numeric($value)) { if (!$this->getCanCreate()) { unset($values[$i]); continue; } $record = new $dataClass(); $record->{$titleField} = $value; $record->write(); $values[$i] = $record->ID; } } if ($values instanceof SS_List) { $values = iterator_to_array($values); } $relation->setByIDList(array_filter($values)); }
/** * {@inheritdoc} */ public function saveInto(DataObjectInterface $record) { parent::saveInto($record); $name = $this->getName(); $titleField = $this->getTitleField(); $source = $this->getSource(); $values = $this->Value(); $relation = $record->{$name}(); $ids = array(); if (!$values) { $values = array(); } if (empty($record) || empty($source) || empty($titleField)) { return; } if (!$record->hasMethod($name)) { throw new Exception(sprintf("%s does not have a %s method", get_class($record), $name)); } foreach ($values as $key => $value) { // Get or create record $record = $this->getOrCreateTag($value); if ($record) { $ids[] = $record->ID; $values[$key] = $record->Title; } } $relation->setByIDList(array_filter($ids)); }