0

I have already inquired here and there but nothing more or less corresponds to my problem.

I have a page with information about a movie, which I access with an id parameter:

<a href="{{path('film', {'id': film.id{){{" class="btn btn-primary"> See comments </a>

The film table having a relation with the table how, I display all the comments specific to the movie thanks to an ArrayCollection :

 $filmRepo = $repo->find($id);

 $comments = $filmRepo->getComments();

I created a CommentController in which I wrote this method whose goal would be to recover the id of the movie AND the id of the comment in order to be able to make CRUD operations on it:

/**
  * @Route("{id}/{comment}/create", name="createComment")
  * @Route("{id}/{comment}/modif", name="modifComment", defaults={"comment"=1}, methods="GET|POST")
  */
    public function modification(Comment $comment = null, Film $film, Request $req, EntityManagerInterface $em) 
    {
        if(!$comment) {
            $comment = new Comment();
        }

        $user = $this->getUser();
        $form = $this->createForm(CommentType::class, $comment);
        $form->handleRequest($req);

        if($form->isSubmitted() && $form->isValid()) {
            $comment->setAuthor($user);
            $comment->setFilm($film);
            $em->persist($comment);
            $em->flush();

            $this->addFlash('success', 'L\'action a bien été effectuée');

            return $this->redirectToRoute('home');
        }

        return $this->render('comment/modif.html.twig', [
            "comment" => $comment,
            "form" => $form->createView()
        ]);
    }

But no matter which comment I select, it takes the default comment, that is to say the one with id 1. So something is wrong with my request. However I pass the two parameters in the twig template:

<a href="{{path('modifComment', {'id' : film.id }, {'comment' : comment.id})}}">Modif</a>
yivi
  • 42,438
  • 18
  • 116
  • 138
gr1ff1th
  • 68
  • 6
  • There is a typo here: `{{path('film', {'id': film.id{` – Felippe Duarte Mar 27 '20 at 17:18
  • What's the actual html in your browser with this piece of code? `Modif` I'm expecting something like ` – Felippe Duarte Mar 27 '20 at 17:22
  • @FelippeDuarte Modifier I have this in my browser – gr1ff1th Mar 27 '20 at 17:27
  • You have `@Route("{id}/{comment}/modif"` so film id = 1 and comment id = 1. Is that what you expect? If not, update your question with the code where you mount the link and should have different `comment.id` than 1 – Felippe Duarte Mar 27 '20 at 17:31
  • @FelippeDuarte It's what a expect for the comment 1 of the film 1. But when i choose a comment 4 of the film 1, for example, that brings me back to comment 1 (which is logical because I put it by default in my modification method – gr1ff1th Mar 27 '20 at 17:42
  • Ok so how is the link for comment 4? It's 1/1/modif or 1/4/modif? The value of `{'comment' : comment.id}` comes from where? – Felippe Duarte Mar 27 '20 at 17:44

1 Answers1

1

The problem comes from a syntax error in the twig template. Instead of :

<a href="{{path('modifComment', {'id' : film.id }, {'comment' : comment.id})}}">Modif</a>

Rather do :

<a href="{{path('modifComment', {'id' : film.id, 'comment' : comment.id})}}">Modif</a>
21baki
  • 82
  • 6