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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

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

SS3 + RESTFulserver: Create / View many_many relations


Go to End


1066 Views

Avatar
goodju

Community Member, 8 Posts

4 October 2013 at 7:33am

Edited: 07/10/2013 10:25pm

I am writing a Javascript Application that uses SS3 (Sapphire only) and the RESTFulServer module as a backbone to view/edit/create/delete multiple DataObjects (no backbone.js application!).
Everything works fine for simple DataObjects. Normal fields, has_one and has_many relations do work pretty well. (saving, reading, deleting, updating)

Now I implemented a new DataObject with a many_many relation to an existing DataObject. The existing DataObject also has the new one as a belongs_many_many relation.

I am able to create and read this new DataObject via the RESTFul API. But when creating a new one, my many_many relations are not written into the many_many table. And when I add the many_many relation to $api_access, I get an error when trying to view the DataObjects via REST:

ERROR [User Error]: Uncaught Exception: Object->__call(): the method 'fortemplate' does not exist on 'ManyManyList'

The $api_access on the new DataObject looks something like this: ("TimeTrackValues" is the many_many relation)

    $api_access = array("Title", "ID", "Created", "MemberID", "TimeTrackValues")

The related DataObjects all have a canView function and are creatable/readable via the RESTFulServer. The only thing that doesnt work is the many_many relation.

Also I am not sure about, how to POST the `TimeTrackValues` ID Array..
My current implementation creates the following "POST Form" request Data in my Webinspector:

Title:my Title
TimeTrackValues[]:45
TimeTrackValues[]:46
TimeTrackValues[]:47
GroupID:17

Comma separating, does not work ( TimeTrackValues:45,46,47 ).

I hope you guys understand what my problem is.
Sadly I couldn't find a documentation on the RestfulServer module that covers many_many relations. Maybe there is one I didn't find?

Thank you very much, I am thankful for any kind of help!
Julian

EDIT

For anyone who might be interested, I found a solution by sending the ID's with a custom property (e.g. 'MyIDs'), catching them up in an onAfterWrite() hook and then adding them manually.