简体   繁体   English

“@ParamConverter 注释未找到 App\Entity\User object”

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

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"当我尝试访问其中一条路线时出现问题,出现此错误:“@ParamConverter 注释未找到 App\Entity\CompanyUser object”

Many people have the same problem but none of the solutions I could see could solve my problem.很多人都有同样的问题,但我能看到的解决方案都不能解决我的问题。

My function edit:我的 function 编辑:

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 !谢谢你的帮助 !

You can override the ParamConverter like this您可以像这样覆盖 ParamConverter

/**
 * @Route("/{id}/edit", name="company_user_edit", methods={"GET","POST"}, requirements={"id"="\d+"})
 * @ParamConverter("id", class="CompanyUser", options={"id": "id"})
 */

The old {id} part of the route has to change to the same name as the parameter you are converting.路由的旧 {id} 部分必须更改为与您正在转换的参数相同的名称。

Ie 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+"})

In my case, the problem was with the wrong router name for the entire controller.就我而言,问题在于整个控制器的路由器名称错误。 I used a generator that did this automatically.我使用了自动执行此操作的生成器。 documents:文件:

/**
 * @Route("/document")
 */
class DocumentController extends AbstractController


/**
 * @Route("/document/group")
 */
class DocumentGroupController extends AbstractController

I changed the name of the router我更改了路由器的名称

@Route("/document/group") 

to

@Route("/document_group")

Symfony 5.4 Symfony 5.4

I have the route to fetch user profile我有获取用户个人资料的路线

routes.yaml路线.yaml
manage-users:
    path: /Account/{id}/Manage
    controller: App\Controller\RegistrationController::manage

When there is a request for a random user id the Object not found error was thrown.当请求随机用户 ID 时,会抛出Object 未找到错误。 I added User object parameter default value as null and included a null check in the controller method.我添加了用户 object 参数默认值为 null,并在 controller 方法中包含了 null 检查。

RegistrationController.php RegistrationController.php
public function manage(User $user = null) <= Default parameter value
{
    try {
        if(null === $user) {
            $this->addFlash("error", "User Not Found");
            return $this->redirectToRoute("home");
        }
     ...
     ...

Hope this is helpful, If anyone is checking this issue.希望这会有所帮助,如果有人正在检查此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @ParamConverter 注释找不到 App\Entity\PlaylistForCompany object - App\Entity\PlaylistForCompany object not found by the @ParamConverter annotation @paramconverter 注释 Symfony 找不到 App\\Entity\\Article 对象 - App\Entity\Article object not found by the @paramconverter annotation Symfony Symfony 4:创建添加实体表单时出错(@ParamConverter批注未找到对象) - Symfony 4 : Error creating an add entity form (object not found by the @ParamConverter annotation) 应用程序:使用 symfony 4 的 @ParamConverter 注释未找到发布 object - App:Post object not found by the @ParamConverter annotation using symfony 4 发行Symfony 3.4“ @ParamConverter批注未找到对象” - Issue Symfony 3.4 “object not found by the @ParamConverter annotation” Symfony 4 - 仅 Profiler 中的 @ParamConverter 注释找不到对象 - Symfony 4 - Object not found by the @ParamConverter annotation in Profiler only Symfony 5 获取失败 object 未通过 @ParamConverter 注释找到 - Symfony 5 get fail object not found by the @ParamConverter annotation Symfony @ParamConverter 通过用户关系获取实体 - Symfony @ParamConverter get entity through user relationship Symfony2 404错误:找不到对象(ParamConverter错误) - Symfony2 404 error: Object Not Found (ParamConverter error) 让@Security注释优先于ParamConverter - Let @Security annotation take precedence over ParamConverter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM