How to handle sub-resources without an ORM object?
mikelovely opened this issue · 1 comments
I have a settings
column in my users table
which is just a string representing a json object. eg
{"foo":"bar","bin":"baz"}
I want an endpoint like /users/{user_id}/settings
which will return an array of all the settings for that user.
How should this look so it's valid json api? I thought maybe something like this:
{
"data": [
{
"type": "settings",
"id": "1",
"attributes": {
"foo": "bar"
}
},
{
"type": "settings",
"id": "2",
"attributes": {
"bin": "baz"
}
}
]
}
That doesn't feel quite right to me because, this way, attributes will only have one key/value pair.
I want to do it this way because I'm also planning to introduce: GET /users/{user_id}/settings/foo
and hope to re-use most of the code.
Part two of this issue... How do I actually parse the settings attribute using Neomerx Api? All the examples I've seen start off working from a defined ORM object eg:
$encoder = Encoder::instance([
'\Author' => '\AuthorSchema',
], new EncoderOptions(JSON_PRETTY_PRINT, 'http://example.com/api/v1'));
echo $encoder->encodeData($author) . PHP_EOL;
For me I don't know what I'm supposed to do here. Something like:
$encoder = Encoder::instance([
\User::settings(???) => '\UserSettingSchema',
], new EncoderOptions(JSON_PRETTY_PRINT, 'http://example.com/api/v1'));
echo $encoder->encodeData($user->settings) . PHP_EOL;
Whenever I try and set the key (\User::settings(???)
) I get back:
Schema is not registered for a resource at path ''."
You've got a few questions )
I have a settings column in my users table which is just a string representing a json object.
Then you are working with it like they are resources (have type
, OK let it be settings
and id
, well, it seems they don't have ID). If you think 'Hold on a sec, I can assign 1,2,3 as array indexes, can't I?' Such identities will not be unique between users.
Within a given API, each resource object’s type and id pair MUST identify a single, unique resource.
from here