Jump to:

10930 Posts in 2613 Topics by 1810 members

All other Modules

SilverStripe Forums » All other Modules » SS3 + RESTFulserver: Create / View many_many relations

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 287 Views
  • goodju
    Avatar
    Community Member
    8 Posts

    SS3 + RESTFulserver: Create / View many_many relations Link to this post

    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.

    287 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.