function testSaveIntoMultiple() { $choices = array('a' => 'a value', 'b' => 'b value', 'c' => 'c value'); $field = new ListboxField('Choices', 'Choices', $choices); $field->multiple = true; // As array $obj1 = new ListboxFieldTest_DataObject(); $field->setValue(array('a', 'c')); $field->saveInto($obj1); $this->assertEquals('a,c', $obj1->Choices); // As string $obj2 = new ListboxFieldTest_DataObject(); $field->setValue('a,c'); $field->saveInto($obj2); $this->assertEquals('a,c', $obj2->Choices); }
function saveInto(\DataObjectInterface $record) { // If tags are enabled, saving into a relation will not work properly if ($this->tags) { $fieldname = str_replace('[]', '', $this->name); $relation = $fieldname && $record && $record->hasMethod($fieldname) ? $record->{$fieldname}() : null; if ($fieldname && $record && $relation && ($relation instanceof RelationList || $relation instanceof UnsavedRelationList)) { $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; } $newIdList = array(); // Tags will be a list of comma separated tags by title $class = $relation->dataClass(); $filterField = 'Title'; $newList = $class::get()->filter($filterField, $idList); $newListMap = $newList->map($filterField, 'ID'); // Tag will either already exist or need to be created foreach ($idList as $id) { if (isset($newListMap[$id])) { $newIdList[] = $newListMap[$id]; } else { $obj = new $class(); $obj->Title = trim(str_replace(self::SEPARATOR, '', $id)); $obj->write(); $newIdList[] = $obj->ID; } } $relation->setByIDList($newIdList); } elseif ($fieldname && $record) { if ($this->value) { if (is_array($this->value)) { $this->value = implode(self::SEPARATOR, $this->value); } $record->{$fieldname} = $this->value; } else { $record->{$fieldname} = null; } } } else { return parent::saveInto($record); } }
public function testSaveIntoManyManyRelation() { $article = $this->objFromFixture('ListboxFieldTest_Article', 'articlewithouttags'); $articleWithTags = $this->objFromFixture('ListboxFieldTest_Article', 'articlewithtags'); $tag1 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag1'); $tag2 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag2'); $field = new ListboxField("Tags", "Test field", DataObject::get("ListboxFieldTest_Tag")->map()->toArray()); $field->setMultiple(true); // Save new relations $field->setValue(array($tag1->ID, $tag2->ID)); $field->saveInto($article); $article = Dataobject::get_by_id('ListboxFieldTest_Article', $article->ID, false); $this->assertEquals(array($tag1->ID, $tag2->ID), $article->Tags()->sort('ID')->column('ID')); // Remove existing relation $field->setValue(array($tag1->ID)); $field->saveInto($article); $article = Dataobject::get_by_id('ListboxFieldTest_Article', $article->ID, false); $this->assertEquals(array($tag1->ID), $article->Tags()->sort('ID')->column('ID')); // Set NULL value $field->setValue(null); $field->saveInto($article); $article = Dataobject::get_by_id('ListboxFieldTest_Article', $article->ID, false); $this->assertEquals(array(), $article->Tags()->sort('ID')->column('ID')); }
/** * @param DataObjectInterface $record * @return void */ public function saveInto(DataObjectInterface $record) { if ($sortField = $this->getSort()) { // If we're sorting, we'll add items to the ManyManyList manually $name = $this->name; $list = $record->{$name}(); $class = $this->dataClass; // Clear the list, we're rebuilding it from scratch $list->removeAll(); // If nothing's been added, that's all we need to do if (empty($this->value)) { return; } // Get our selected items $selectedList = $class::get()->byIDs(array_values($this->value))->toArray(); // Convert our selected items to an ID => Object associative array $selected = array(); foreach ($selectedList as $item) { $selected[$item->ID] = $item; } // Now loop through the selected items (as these are in the correct order) and populate the list foreach ($this->value as $order => $id) { $item = $selected[$id]; $list->add($item, array($sortField => $order)); } } else { // If we're not sorting, ListboxField can handle saving the items parent::saveInto($record); } }