How do you write form data into many_many relationships?

915 views
Skip to first unread message

TomDude48

unread,
Feb 24, 2008, 1:36:39 PM2/24/08
to SilverStripe Development
I have two objects, Post & Category, in a many_many relationship to
hold user data submitted via a form. The Categories are included in
the form as a dropdownField. I have got it writing form data into the
Post object, but how do I get SS to write the Post_Categories
relationship? Any help is appreciated.

Classes:
class Posts extends DataObject {
...
static $many_many = array(
"Categories" => "Category"
);
static $many_many_extraFields = array(
"Categories" => array(
"PrimeCategory" => "Boolean"
)
);
...
}
class Category extends DataObjec t {
...
static $belongs_many_many = array(
"CModules" => "CModule",
"Posts" => "Post"
);
...
}

class Form_Controler extends Page_Controler {
...
function PostForm() {
...
$cats = $this->Categories();
if(count($cats->getIdList())) {
$catOptions = array();
foreach($cats as $cat) {
$catOptions[$cat->ID] = $cat->Category;
}
$fields->push(new DropdownField('Categories',"Category",
$catOptions));
...
}

function doPostForm($data,$form) {
$post = new Post();
$form->saveInto($post);
$post->write();
// ? how do I write to Post_Categories?
...
}

Sean Harvey

unread,
Feb 24, 2008, 3:20:29 PM2/24/08
to silverst...@googlegroups.com
I believe this should automatically be created for you. It should be
taking the array data from the dropdown 'Categories' and putting it
into Post.

If I'm not mistaken, $form->saveInto($post) and $post->write() should
be doing that work for you.

Sean

Sean Harvey | Developer
SilverStripe
http://silverstripe.com

Phone: +64 4 978 7330 ext 39
Skype: halkyon5

Level 3, 97-99 Courtenay Place
Wellington, New Zealand

TomDude48

unread,
Feb 24, 2008, 3:41:02 PM2/24/08
to SilverStripe Development
That is what I thought, however the standard SaveInto/write is not
working. It does save the data to post, just not the post_categories
relationship. Perhaps I am not formatting the select dropdown properly
or it is the fact that I added a many_many_extraFields of
PrimeCategory to the relationship causes it not to work.

I am using this to create the DropdownField

$cats = $this->Categories();

Sean Harvey

unread,
Feb 24, 2008, 4:42:20 PM2/24/08
to silverst...@googlegroups.com
Rather than looping through each of the "cats", you might want to do
this instead:

$cats = $this->Categories();
if($cats) $catsSource = $cats->toDropDownMap('ID', 'Title');

This takes the 'ID' and 'Title' fields from the Category record, and
builds an array map of ID => Title for the dropdown source.

Not sure if this will have any effect on the current problem, but it's
a good way of formatting source data for dropdowns.

Also, to manually save the categories related data you can call $cats-
>add($category) where $category is a Category record you want to add
to the relation. add() is a method on ComponentSet you can call to
manage your relation. Likewise, you can use remove(). Not sure why
this isn't working by default on saveInto() though...

Cheers,
Sean

nor...@silverstripe.com

unread,
Feb 24, 2008, 6:29:18 PM2/24/08
to SilverStripe Development
I don't think saveInto can deal with saving many_many relations
currently, unless your Post class has a customised method called
saveCategories($data) in which you simply add the code like this:

$this->Categories->add($item, $extraFields),
where $item should be the category's ID (you can get it from $data),
depending on how your Form is stuctured, your $extraFields should be
either from $data, or assigned from backend by you before you call
saveInto in doPostForm or before you call $this->Categories->add
saveCetegories.

The name of saving many_many relation (here refer to saveCategories)
must bond with your field name, ie, if your field is called:
new DropdownField('GoneWithWind',"Category", $catOptions), the save
method must name as saveGoneWithWind().

if PrimeCategory need to be also submitted from from, your your can
get CategoryID and PrimeCategory in an array from $data, try this:
new DropdownField('Categories[CetegoryID]',"Category", $catOptions),
new CheckboxField('Categories[PrimeCategory]', "Prime Category?"),
The $data[Categories] submitted from from is a array.

In addition, dropdown field is not supposed to simulate many_many
relation, CheckboxSetField is aim to submit multiple IDs from a form
(maybe you have you special consideration here?). If this is the case,
$this->Categories->add need to change to $this->Categories-
>addMany($IDs). You can find more details about these methods in
http://api.silverstripe.com/ under section of "Sapphire" and
"Form" (Form.php and ComponentSet.php are most recommanded to read).


On Feb 25, 7:36 am, TomDude48 <tmccrac...@lorentzconsulting.com>
wrote:
Reply all
Reply to author
Forward
0 new messages