-
-
Notifications
You must be signed in to change notification settings - Fork 893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Laravel] POST/PUT/PATCH don't work for embedded relations #6882
Comments
This seems a bit clunky, since in laravel you usually do not write your model attributes as properties, hard to tell what is the intended way here. @soyuka would love to here your input on how all of that. EDIT: Thinking about it, this might also add some other problems and rabbit holes like the security arround all of that... EDIT2: Removed my confusing examples, already explained well enough above. |
Happy to know I am not the only one facing this issue @toitzi so it's not my bad :) Current SituationAfter digging into the code a little bit, IMO the key is the PersistProcessor: RelationsPersistProcessor only processes BelongsTo Relations so any other relation is processed as it was an attribute of that Model, that's why when trying to save a HasMany Relation "Unknown column in 'field list'" DB's error is returned. Persist Related ModelsAt the same time, the only action performed is associate an existing Model so it is not creating a new Model on POST operations (maybe PUT operations should also?) neither no change on that associated Model on PATCH operation is persisted as I guess SuggestionsRelationsPersistProcessor should also process other Relations:
Persist Related ModelsOperations should persist related Models:
Do you agree @toitzi ? What are your thoughts @soyuka? |
Writing embeded relations has always raised many issues we don't recommend this at all. Instead you should work on the resource itself. For example: POST Same goes for patching etc. Note that by not doing so, performances are horrible. Also, having an embed collection you won't be able to control how it is paginated, therefore we recommend to use URIs for your collection relations:
This solves all issues, more on the subject at https://edge-side-api.rocks/. Now as it may be convenient (especially for graphql I guess), I'm okay to merge patches to the PersistProcessor to support the |
Thanks for the answer @soyuka. So if I understood correctly that means denormalization as it is explained on the documenctation doesn't apply on Laravel so we should manage the relations not altogether but the resources itself independently, right? I understand the headeache with relations but that means we will need at least 1 call for each relation, so if we want to create 1 Model that has 5 relations then 6 calls will be needed (1 for the Model itself and 1 for each relation in case we need to create those Models also). Not sure what is better in terms of performance, plus the complexity of managing errors and recalls if some of those relations fail. Is there no other alternative? |
I thought that denormalization was working indeed, at least it should. I need to investigate this then, I thought only the Persistor was an issue here that should be fixable! |
Thank you so much @soyuka , let me know if there is anything I can help on. |
API Platform version(s) affected: 4.0.12
Description
When trying to create, replace or update an embbeded relation using POST, PUT or PATCH Operations, that embedded relation is not modified.
As far as I know, using denormalization we should be able to create, replace or update and embbeded relation using its fields instead of IRI.
How to reproduce
Having a couple of simple Models,
Father
andSon
, defined as following:and
Created using following migrations:
and
Examples:
FATHER
Using POST on
Father
:It is supposed to create 1 new
Father
and 2 newSon
but i returns:The same if instead of creating 2 new
Son
I just create a new father and relate it with 2 existingSon
:The same using IRI:
SON
Son
with a newFather
.Using POST on
Son
It is supposed to create 1 new
Son
and a newFather
but a newSon
with noFather
is created.Creating a new
Son
related to an existingFather
using IRI:It actually creates the new
Son
with the proper existingFather
Father
's name fromSon
.Using PATCH on
Son
with id = 1:The father's name does not change.
I didn't add all the possibilities using POST, PATCH and PUT but I think the problem can be understood with the previous examples.
Possible Solution
On a Symfonycasts course API Platform 3 Part 1: Mythically Good RESTful APIs I've seen something called
cascade: ['persist']
here and here not sure how to reproduce it on Laravel if it is needed.Also, I am not sure if some kind of additional denormalization is needed neither because I didn't see anything related on Documentation, although I am aware Larave'ls one still need some improvements. In that case, could someone explain how to do it?
Thank you
The text was updated successfully, but these errors were encountered: