silverstripe / silverstripe-restfulserver

RestfulServer module for Silverstripe CMS
http://www.silverstripe.org/restfulserver-module/
BSD 3-Clause "New" or "Revised" License
45 stars 48 forks source link

relationship returns no data #61

Closed guyvanbael closed 6 years ago

guyvanbael commented 6 years ago

The data from my many_many relation (Personas) returns an empty array in the json object

"size1": "6", "size2": "6", "PhotoID": "4", "Personas": [] },

NightJar commented 6 years ago

Hi @guyvanbael Thanks for reporting. Are you able to also please supply the versions of the module and framework you are working with, and perhaps some steps to recreate? composer show [package] can help you with this if you're not sure how :)

guyvanbael commented 6 years ago

@NightJar It's version 2.0.3 on SS4.1

Making the call (in Postman) return empty array Personas

        {
            "ID": "1",
            "ClassName": "TextImageLeft",
            "LastEdited": "2018-04-25 15:12:00",
            "Created": "2018-04-25 15:11:04",
            "Title": "Blok 1",
            "ShowTitle": "0",
            "Sort": "1",
            "ExtraClass": "",
            "Style": "",
            "Version": "5",
            "ParentID": "1",
            "Text": "<p>Hier de inhoud van blok 1</p>",
            "size1": "6",
            "size2": "6",
            "PhotoID": "4",
            **"Personas": []**
        },

... and i don't know if it's related, but when add a filter in the front-end app (vue.js), Filtering on a relation doesn't work. E.g. for querying all block of a certain page.

new Vue({
            el: '#app',
            data: {
                pagecontent: [],
                status: 'loading...',
                nrofmarks: ''
            },
            created() {
                this.getpagecontent(1);

             },
            methods: {
                getpagecontent: function(pageID){
                    var vm = this
                    var config = {
                    headers: {'Accept': 'application/json'},
                    params: {ParentID: pageID}
                    };
                    axios.get('http://localhost:8888/testss41/api/v1/DNADesign-Elemental-Models-BaseElement', config)
                    .then(function (response) {
                        vm.pagecontent = response.data;
                        console.log(vm.pagecontent)
                    })
                },

            }
        });
robbieaverill commented 6 years ago

@guyvanbael I've tried to reproduce this issue (admittedly quickly) and can't:

class Page extends SiteTree
{
    private static $many_many = [
        'Members' => Member::class,
    ];

    private static $api_access = true;

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab('Root.Members',
            new GridField('Members', 'Members', $this->Members(), GridFieldConfig_RelationEditor::create())
        );

        return $fields;
    }
}
SilverStripe\Security\Member:
  api_access: true
$ curl http://mysite.localhost/api/v1/Page/7.json | jq

...
  "Members": [
    {
      "className": "SilverStripe\\Security\\Member",
      "href": "http://mysite.localhost/api/v1/SilverStripe\\Security\\Member/1.json",
      "id": 1
    },
    {
      "className": "SilverStripe\\Security\\Member",
      "href": "http://mysite.localhost/api/v1/SilverStripe\\Security\\Member/9.json",
      "id": 9
    }
  ],
...

And a full list of pages:

$ curl http://mysite.localhost/api/v1/Page.json | jq
...
"Members": [
        {
          "className": "SilverStripe\\Security\\Member",
          "href": "http://mysite.localhost/api/v1/SilverStripe\\Security\\Member/1.json",
          "id": 1
        },
        {
          "className": "SilverStripe\\Security\\Member",
          "href": "http://mysite.localhost/api/v1/SilverStripe\\Security\\Member/9.json",
          "id": 9
        }
      ],
...

On a side note, have you looked into using the GraphQL module for integration with your Vue app?

guyvanbael commented 6 years ago

added the member class to the api and linked some members to a page as you did, but when i do a call to page, the relation Member isn't even in the results { "ID": "1", "ClassName": "Page", "LastEdited": "2018-04-25 15:09:30", "Created": "2018-03-14 12:27:55", "URLSegment": "home", "Title": "Home", "MenuTitle": "Home", "Content": "<p>Welcome to SilverStripe! This is the default homepage. You can edit this page by opening <a href=\"admin/\">the CMS</a>.</p><p>You can now access the <a href=\"http://docs.silverstripe.org\">developer documentation</a>, or begin the <a href=\"http://www.silverstripe.org/learn/lessons\">SilverStripe lessons</a>.</p>", "MetaDescription": "", "ExtraMeta": null, "ShowInMenus": "1", "ShowInSearch": "1", "Sort": "1", "HasBrokenFile": "0", "HasBrokenLink": "0", "ReportClass": "", "Version": "2", "CanViewType": "Inherit", "CanEditType": "Inherit", "OGTitle": "", "OGContent": "website", "OGDescription": "", "Priority": "", "ParentID": "0", "OGImageID": "0", "ElementalAreaID": "1", "VirtualPages": [] },

guyvanbael commented 6 years ago

@robbieaverill i'm definitely gonna checkout graphql (after seeing Uncle Cheese's talk about that). He told me on slack that more docs on how to use it are coming up in the near future. I'm very looking forward to this.

robbieaverill commented 6 years ago

@guyvanbael I'm going to close this issue since I can't reproduce it, but feel free to reopen it again if you find more information to help reproduce the root of the problem =)

guyvanbael commented 5 years ago

@robbieaverill i got this working, but is there a way to get all the fields from the has_many relationship instead of only the id, classname and href. If yes, it would save a lot of api-calls

NightJar commented 5 years ago

@guyvanbael it might be worth while looking into how GraphQL works, as it might be more appropriate for your use case :)

phptek commented 5 years ago

@guyvanbael FYI, a similar request to what you're asking for can be found in #69