问题
I have a problem with one of my route when I try to access it, I have this error : "App\Entity\CompanyUser object not found by the @ParamConverter annotation"
Many people have the same problem but none of the solutions I could see could solve my problem.
My function edit :
private $passwordEncoder;
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
    $this->passwordEncoder = $passwordEncoder;
}
/**
 * @Route("/{id}/edit", name="company_user_edit", methods={"GET","POST"}, requirements={"id"="\d+"})
 */
public function edit(Request $request, UserPasswordEncoderInterface $passwordEncoder, CompanyUser $user): Response
{
    $em = $this->getDoctrine()->getManager();
    $user = $this->getUser();
    $reset_password_form = $this->get('form.factory')->create(CompanyResetPasswordType::class, $user);
    $reset_password_form->handleRequest($request);
    if ($reset_password_form->isSubmitted() && $reset_password_form->isValid()) {
        $this->passwordEncoder->isPasswordValid($this->getUser(), $request->get('password'));
        $oldPassword = $request->request->get('reset_password')['oldPassword'];
        // Si l'ancien mot de passe est bon
        if ($passwordEncoder->isPasswordValid($user, $oldPassword)) {
            $newEncodedPassword = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
            $user->setPassword($newEncodedPassword);
            $em->persist($user);
            $em->flush();
            $this->addFlash('notice', 'Succeed to change your password');
            return $this->redirectToRoute('company_show', ['id' => $user->getCompany()->getId()]);
        } else {
            $reset_password_form->addError(new FormError('Old password is not valid'));
        }
    }
    $edit_form = $this->createForm(CompanyUserType::class, $user);
    $edit_form->handleRequest($request);
    if ($edit_form->isSubmitted() && $edit_form->isValid()) {
        $this->getDoctrine()->getManager()->flush();
        return $this->redirectToRoute('company_show', ['id' => $user->getCompany()->getId()]);
    }
    return $this->render('user/edit.html.twig', [
        'user' => $user,
        'edit_form' => $edit_form->createView(),
        'reset_password_form' => $reset_password_form->createView(),
    ]);
}
Thanks for your help !
回答1:
You can override the ParamConverter like this
/**
 * @Route("/{id}/edit", name="company_user_edit", methods={"GET","POST"}, requirements={"id"="\d+"})
 * @ParamConverter("id", class="CompanyUser", options={"id": "id"})
 */
回答2:
The old {id} part of the route has to change to the same name as the parameter you are converting.
I.e. change:
 * @Route("/{id}/edit", name="company_user_edit", methods={"GET","POST"}, requirements={"id"="\d+"})
To
 * @Route("/{user}/edit", name="company_user_edit", methods={"GET","POST"}, requirements={"id"="\d+"})
来源:https://stackoverflow.com/questions/55516176/app-entity-user-object-not-found-by-the-paramconverter-annotation