Symfony 5 how to automatically send the current user in an update with a ManyToMany relationship

I want my current user to enroll in a rate.

```
/**
 * @ORMColumn(type="string", length=255)
 */
private $name;

/**
 * @ORMManyToOne(targetEntity="AppEntityUser", inversedBy="matters")
 * @ORMJoinColumn(nullable=false)
 */
private $user;

/**
 * @ORMOneToMany(targetEntity="AppEntityNote", mappedBy="matter")
 */
private $notes;

/**
 * @ORMManyToMany(targetEntity="AppEntityUser", inversedBy="studentsMatters")
 */
private $student;
```

When he presses “update”, the ManyToMany relationship must send the current user.
In my controller, I am trying to send the current user

/**
     * @Route("/{id}/edit", name="student_edit", methods={"GET","POST"})
     */
    public function edit(Request $request, Matter $matter): Response
    {
        $form = $this->createForm(StudentType::class, $matter);
        $form->handleRequest($request);
        $user = $this->getUser();
        $matter->setStudent($this->getUser());

        if ($form->isSubmitted() && $form->isValid()) {       
            $this->getDoctrine()->getManager()->flush();

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

        return $this->render('student/edit.html.twig', [
            'matter' => $matter,
            'form' => $form->createView(),
        ]);
    }

but it doesn’t works.

How I do to get current user?

Source: Symfony Questions

Was this helpful?

1 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *