xiaohutai / jsonapi

[Bolt Extension] JSON API for Bolt CMS
https://market.bolt.cm/view/bolt/jsonapi
MIT License
55 stars 18 forks source link

Error when getting relationships #65

Open rainpoiss opened 7 years ago

rainpoiss commented 7 years ago

/{ct}/{id}/{relatedtype} works only one way. Example I have ct pages and ct categories i choose relationship when creating new page. it works when i make request /pages/1/categories but when i try to use it the other way: categories/1pages/ i get the error:

Uncaught Exception: StorageException .

StorageException in Relations.php line 205: Unable to load collection values. Ensure that EntityManager is set on Bolt\Storage\Collection\Relations

xiaohutai commented 7 years ago

Have you explicitly defined both relationships in contenttypes.yml? As in, does the contenttype categories have a relation to pages (and vice versa)?

rainpoiss commented 7 years ago

yes I have under categories: relations: pages: multiple: true label: Sisuleht order: -id

under pages:

relations: categories: multiple: false label: Kategooria order: -id

I have a discovered when i delete relation under categories and add it there again, then it works... But default it apperars under categories.

rainpoiss commented 7 years ago

the following table structure


"id","from_contenttype","from_id","to_contenttype","to_id"
2,"sections",1,"pages",1
3,"pages",2,"sections",1
4,"pages",3,"sections",1

gives me this response in API

{
    "links": {
        "self": "http:\/\/localhost\/json\/sections\/1",
        "sections": {
            "href": "http:\/\/localhost\/json\/sections\/2\/sections",
            "meta": {
                "count": 1
            }
        },
        "pages": {
            "href": "http:\/\/localhost\/json\/sections\/3\/pages",
            "meta": {
                "count": 1
            }
        }
    },
Raistlfiren commented 5 years ago

Hi, As of right now it seems as though Bolt is not getting the relationship properly, or I am using it incorrectly. The error lies with the code - https://github.com/xiaohutai/jsonapi/blob/master/src/Action/SingleAction.php#L49

When attempting to get related content, it calls the internal Bolt method getRelation() and only attempts to checks the to_contenttype. At this time there is no way to set bidirectional to true. This is the line in question - https://github.com/bolt/bolt/blob/c3ead6d48cc2431d72ae7f81947df7fa5fefa92b/src/Storage/Collection/Relations.php#L224

@rossriley - Are the Bolt internals functioning correctly, or is there a problem with the my code? getRelation($contentType)->count() is always returning 0 on inverse relationships. Shouldn't bi-drecirectional relationships always default to true since all Bolt relationships are supposed to be that? Here is what my relationship looks like in Bolt - screen shot 2019-03-06 at 4 45 45 pm