“@ParamConverter 注释未找到 App\Entity\User 对象"; [英] "App\Entity\User object not found by the @ParamConverter annotation"

查看:78
本文介绍了“@ParamConverter 注释未找到 App\Entity\User 对象";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试访问它时,我的其中一条路由出现问题,出现此错误:@ParamConverter 注释未找到 App\Entity\CompanyUser 对象"

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.

我的功能

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(),
    ]);
}

感谢您的帮助!

推荐答案

你可以像这样覆盖 ParamConverter

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"})
 */

这篇关于“@ParamConverter 注释未找到 App\Entity\User 对象";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆