api-platform icon indicating copy to clipboard operation
api-platform copied to clipboard

Link doctrine entities in API PLATFORM

Open ziiko10 opened this issue 6 years ago • 7 comments

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.

ziiko10 avatar Dec 04 '19 09:12 ziiko10

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.

dunglas avatar Dec 17 '19 07:12 dunglas

@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"

teohhanhui avatar Dec 17 '19 15:12 teohhanhui

Ok thank you guys

ziiko10 avatar Dec 17 '19 20:12 ziiko10

@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 ?

ziiko10 avatar Dec 17 '19 23:12 ziiko10

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.

teohhanhui avatar Dec 23 '19 15:12 teohhanhui

But we support JSON Merge Patch: https://tools.ietf.org/html/rfc7396

(API Platform 2.5+)

teohhanhui avatar Dec 23 '19 15:12 teohhanhui

Closing per #2987. Questions are best asked on GitHub Discussions or Stack Overflow (api-platform tag). The framework now lives at api-platform/core.

soyuka avatar May 19 '26 08:05 soyuka