Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

MultiValueField


Reply


2 Posts   227 Views

Avatar
Lukereative

Community Member, 8 Posts

20 January 2014 at 6:48pm

Edited: 20/01/2014 6:50pm

I am currently trying to develop a website using SilverStripe with the silverstripe-multivaluefield module.
I managed to mostly figure out how to wrangle it to do what I wanted but I ran into an issue when I use the data from a MultiValueField to populate a MultiValueCheckboxField.

What I am trying to achieve is being able define categories for a set of objects (for example books able to be set to Fantasy or Action), these books are stored as data objects (SortableObject) and are related to a page (SortableObjectPage).

See SortableObjectSection.php
I first set the categories using a MultiValueTextField on the SortableObjectPage and store the values in MultiValueField.

$fields->addFieldToTab('Root.Main', new MultiValueTextField('Categories'), 'Metadata’);

See 30.png

And it is stored in the database like so

See 31.png

See SortableObject.php
I then access the ‘Categories’ MultiValueField and use it to populate a MultiValueCheckboxField when creating a SortableObject

$parentCategories = $this->SortableObjectSection()->Categories->Value;
$categoriesField = new MultiValueCheckboxField('RelatedCategories', 'Related Categories', $parentCategories);

See 32.png

This is how it’s stored in the database

See 33.png

This is where my problem is, instead of storing the strings such as ‘Fantasy’ or ‘Action’ it simply stores a number (I think it’s the index of the category in the array used to populate the Checkboxes) as the string in the database, which means I can’t access the strings on the template I am trying to make.

I have also attached the two files that define SortableObjectSection and SortableObject as txt files but they are usually php files.

Attached Files
Avatar
Lukereative

Community Member, 8 Posts

21 January 2014 at 2:14pm

This was resolved with an update to the module after talking to the repository maintainer.
I was able to add:

$categoriesField->setStoreKeys(true);


To the MultiValueCheckboxField and this meant that the proper strings were stored.