What is the correct way to create an object that will be associated with others? I cannot find a source of information about how this should be done or how it is done in Symfony with the FOSRestBundle package.
Reference Information:
Allows you to have a Car object with Wheel objects.
How to create a car with wheels on one request?
url: foo.com/cars POST
data: {car:{name="porsche", wheels:[{name:"fr"}, {name:"fl"}] }}
Is this right how to do it? I read a few things about LINK and UNLINK http methods, but for this you need to send a few requests to create all the wheels and then link them together.
How to create a wheel with several cars?
url: foo.com/wheels POST
data: {wheel:{name="super wheel", cars:[{id:1}, {id:2}] }}
In this situation, it is advisable to use the LINK header. But after reading this article, this will require parsing LINK headers for all POSTS and PUTS, which makes it expensive and cumbersome.
EDIT
I wrote to the author of the mentioned article. Here is his answer:
I think you can send data containing both your wheels and cars in one request. The most important thing here is to be pragmatic. There are no “clear” rules for building a REST API.
LINK / UNLINK are useful when resources already exist and you want to “link” them, in other words, you want to add a “relationship” between them, for example, friendship for users.
About your second question, if there are cars, you can, firstly, create your own wheel and “tie” it to your cars. But this will mean two requests for this action (you can add several link headers in one request), or better you could send one POST request with link headers: one request for all of them: p
Another option would be to link to the vehicle IDs in your wheel data. I think this is good too.
rest symfony hyperlink entity fosrestbundle
Keo
source share