"App\Entity\User object not found by the @ParamConverter annotation"

16,007

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"})
 */
Share:
16,007

Related videos on Youtube

BanjoSf4
Author by

BanjoSf4

Updated on June 04, 2022

Comments

  • BanjoSf4
    BanjoSf4 almost 2 years

    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 !