本文整理汇总了PHP中ListboxField::saveInto方法的典型用法代码示例。如果您正苦于以下问题:PHP ListboxField::saveInto方法的具体用法?PHP ListboxField::saveInto怎么用?PHP ListboxField::saveInto使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ListboxField
的用法示例。
在下文中一共展示了ListboxField::saveInto方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: saveInto
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);
}
}
示例2: testSaveIntoManyManyRelation
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'));
}
示例3: saveInto
/**
* @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);
}
}
开发者ID:helpfulrobot,项目名称:fullscreeninteractive-silverstripe-multiselectfield,代码行数:34,代码来源:MultiSelectField.php