neomerx / json-api

Framework agnostic JSON API (jsonapi.org) implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use "to Many" Relationships in Schema

sebtoombs opened this issue · comments

commented

When attempting to encode a resource that has a relationship to a single other resource (i.e. what is often referred to as a "toOne" resource) the Parser throws the following error:

Schema is not registered for a resource at path "toOneResource"

Note that the error only occurs when the relationship is not empty. If the relationship is empty, the data is generated correctly. (e.g. below)

data: {}

instead of

data: []

Is this a bug, or are "toMany" relationships not supported?

It supports both types of relationships to single resource and collections. Encoder understands that array, Iterator or Traversable are toMany relationships and acts accordingly. You just provide your data in Schema and encoder does the rest. That's an example for both types of relationships and how encoding result looks like.
Apart from numerous encoding samples there is a wiki as well.

commented

Thanks, I had already reviewed all of that information.

Turned out the issue was with types, Doctrine returning a proxy but the Schema expecting an entity.

Thanks for the reply.

Long long time ago issue with doctrine proxies was solved in #40

I'm closing the issue. If you have any further questions please don't hesitate to ask.