Link doctrine entities in API PLATFORM
Hello everyone, i want to link a Category entity with a Product one and i want to know if i should use a custom operation to do that or there is a better way to do that ?
* "LINK-CATEGORY-TO-PRODUCT"={
* "method"="PUT",
* "path"="/products/{id}/categories/{categoryId}",
* "controller"="App\Controller\LinkCategoryToProductController",
* "access_control"="is_granted('LINK', previous_object)",
* "openapi_context"= {
* "summary"="Link and unlink a product to a category"
* }
* },
I really dont think that is the best way to do relations because if I have a big project with a lot of relationships I can't afford to do a custom operation per relationship.
It's not a very RESTful approach. You should probably sent the PUT request (or a PATCH request) on the /products/{id}/ URL directly to modify the underlying resource.
@AbbesBendaoud10 I believe we have discussed this on Slack. I thought you were asking about the LINK method? https://tools.ietf.org/html/draft-snell-link-method-12
An example might be:
LINK /products/1/categories HTTP/1.1
Host: api.example.com
Link: </categories/1>; rel="item"
Ok thank you guys
@teohhanhui But when i specify a new operation with LINK method , i have this error "There is no builtin action for the collection LINK operation. You need to define the controller yourself" Are you sure Link and Unlink are available in api-platform ?
It's not supported out of the box in API Platform. You can build it yourself. :smile:
It would be something good to have as a new feature! Feel free to send a PR targeting master branch.
But we support JSON Merge Patch: https://tools.ietf.org/html/rfc7396
(API Platform 2.5+)
Closing per #2987. Questions are best asked on GitHub Discussions or Stack Overflow (api-platform tag). The framework now lives at api-platform/core.