-
Notifications
You must be signed in to change notification settings - Fork 57
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
Best practices to work with relations in Api Platform 3 and DDD #31
Comments
Another question I asked myself is if I should use groups and normalization/denormalization contexts to post an identifier and get the resource of my relation. It's a littlebit confusing DDD as explained in the workshop and relate my problem with the actual documentation that don't speak about api platform and DDD. |
Bonjour tomasattifabio, je me retrouve actuellement avec les mêmes problématiques que vous. |
Any update on it from anyone so maybe I will not be reinventing the wheel? I have a some relations between my entities That's my second attempt into going with DDD as it's really gives what I want - isolation and single responsibility comparing to what we have OOTB approach. |
@Jarzebowsky Have you by chance watched Ryan Weaver's latest SymfonyCasts that is essentially a deep dive into the marriage of APIPv3 & DDD -- the core topic of this thread? |
@qualeo can you link the page please? |
@qualeo @cedricvazille Thank you for linking those. Perhaps that will solve my problem of isolation. |
I have two entities in a one-to-many relation.
To refer to your example of the BookStore imagine that a Book has a Category in a one-to-many relation. A Category is in relation with many Book entities.
I have created a sort of "copy" of the book store for the folders and files structure for both my two entities, with create commands, other commands, item and collection provider, repositories, resources, etc etc. Same as the book store.
My problem is in the creation of a Book by BookResource and the various commands and command handlers when I have to save the related Category at the same time. Let me explain with the code.
The BookResource has the POST operation defined like this:
Your BookResource class is the seguent:
Now, how can I define a new property to associate the category to the book? Which is the type that I must assign to the category property?
My question is about what I see in the swagger and in which way I define and use the property in the command and command handler.
If I define category only as the category id, for example a string for uuid, the swagger show me a thing like this in the request body:
If I accept the category as a string, for example the uuid, then I receive the uuid in the $data in your CreateBookProcessor.
The processor make a new CreateBookCommand to dispatch to the command bus. The command is defined like this.
The CreateBookCommandHandler is defined like this:
To use the category id received in the processor I think that I have to add in the create command a $category property of type CategoryId that is the category id value object like this:
If I add the category like this, I have to initialize the command like that in the processor.
The command handler is now a big problem because I don't know in which way I can save the category to the new book because the Book entity has the Category public property and not a string like the uuid received in the handler.
My actual solution is to find the category with the provided id by the category reporitory and set the found object in the Book entity. The use the book repository to save the new book.
With this code I can save correctly the category to the book but with the category defined like this in all the classes I have my big problem when getting the data. Like the processor, the state provider uses the
fromModel
method of the BookResource. The method return a new self object and this is the problem because I defined the category as a string and in the swagger result I only can send a string and nothing else, like the Category object for example.How can I accept a string in the category property in the book resource and send back the IRI of the associated Category when I retrieve a book item or books collection?
If I define the category property in the book resource as a Category object, and then in all the other classes, I see all the Category properties in the POST request body and I don't want to create a new category when I create a new Book. I only want to create the new book and associate the category to it! This last solution send in output the whole category in the book single item but I don't want this solution because it's not the IRI at the same way of the first solution!
If it's not clear ask me!
The text was updated successfully, but these errors were encountered: