Skip to content

Include array of Identifiers  #235

@untone-survive

Description

@untone-survive

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?

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions