neomerx / json-api

Framework agnostic JSON API (jsonapi.org) implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Include array of `Identifier`s

h8every1 opened this issue · comments

I have a resource that has many child resources of the same type (e.g. category that has many subcategories), to prevent recursion-related problems (we provide both parent and children relationships) I pass only subcategories IDs and not the whole resource object in Schema's getRelationships().

I've tried to use array of Identifiers, but got error

No Schema found for resource `Neomerx\\JsonApi\\Schema\\Identifier`

which is frustrating. Now I create semi-fake resource objects, that contain only IDs to get identifier objects in json:

        if (count($resource->getChildren())) {
            $childResources = [];

            foreach ($resource->getChildren() as $childId) {
                // create fake resource to provide ID
                $childResources[] = new Category(['id' => $childId]);
            }

            $result['children'] = [
                self::RELATIONSHIP_DATA => $childResources,
                self::RELATIONSHIP_LINKS_SELF => false,
            ];

        }

        return $result;

Is this the desired way?

Hi, that looks like a bug. Identifiers should work just fine in arrays as not in arrays. I've made some changes in develop branch and it should work now. Please have a look and let me know if on your side it works as well.

BTW, I believe the lib can work with resources that have parent and children interrelationships just fine. It's covered by tests.

I haven't received any feedback from you on the fix for some time. Let me know if there are any issues remaining.

Fix released in v3.0.4.

Sorry for the delay. Everything seems to be working OK now.