Kevin avatar Kevin 3 years ago

Hiya,

I was testing out the ManyToMany relationship using my own created examples (Post and Category) and I was wondering if this was the correct way of saving it to the db with a form.

What I did was made a post creation form and rendered a list of category names (checkboxes) from the db using EntityType. Then I processed it in PostController by looking up the the category based on the ID(s) that was coming from the POST request and saving it by using $post->addCategory(...).

Post creation form: https://i.imgur.com/hDE0sK3...

Post controller: https://i.imgur.com/UNI6MZn...

| Reply |

Hey Ccc123,

Looks like you nailed it, good job! But you don't leverage Symfony Forms in your code completely :) I mean, you use Symfony Forms to render the form but you don't handle the form in Symfony way. We have a separate course about Symfony forms: https://symfonycasts.com/sc... - I think you might be interested in it. In particular, here's an example how to handle Symfony forms in controllers: https://symfonycasts.com/sc...

Cheers!

1 | Reply |

Thank you!

| Reply |