Skip to content
Advertisement

Symfony 5 get fail object not found by the @ParamConverter annotation

I have a form for add users on my event, but when I add the user I try to add a role on this event, but I get this fail when the second function is executed

AppEntityEvento object not found by the @ParamConverter annotation.

I don’t see the problem, thanks

public function registro(Request $request, UserPasswordEncoderInterface $encoder, Evento $evento)
    {
        $evento = $this->getDoctrine()->getRepository(Evento::class)->findOneById($evento);
        $user = new user();
        $form = $this->createForm(RegisterUsuarioEventoType::class, $user);
        //rellenar el objeto con los datos del formulario
        $form->handleRequest($request);
        //comprobando si el formulario se ha enviado
        if($form->isSubmitted() && $form->isValid()){
            //modificando objeto para guardarlo
            $user->setRole('ROLE_USER');
            $user->setFecha(new DateTime('now'));
            $encoded = $encoder->encodePassword($user, $user->getPassword());
            $user->setPassword($encoded);
            $user->addEvento($evento); 
            $evento->addUser($user);
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();
            return $this->redirect($this->generateUrl('registro2', ['id' => $user->getId()]));
        }
        return $this->render('evento/register-evento.html.twig', [
            'form' => $form->createView()
        ]);
    }

My second function is executed when the first one flush on my database,

    public function registro2(Request $request, Evento $evento)
    {
        $userhasevento = new UserHasEvento();
        $form = $this->createForm(RegisterUsuarioEvento2Type::class, $userhasevento);
        $form->handleRequest($request);        
        if($form->isSubmitted() && $form->isValid()){
            $contactFormData = $form->getData();

            $userhasevento->SetTipoinvitado($contactFormData['tipo'],'text/plain');
            $userhasevento->SetFechainscripcion(new DateTime('now'));
            $userhasevento->SetEventoid($evento->getId());
            $userhasevento->SetUserid($user->getId());

            $em = $this->getDoctrine()->getManager();
            $em->persist($userhasevento);
            $em->flush();
            
            return $this->redirect($this->generateUrl('detalle', ['id' => $evento->getId()]));
 
        }        

        return $this->render('evento/AsignarEvento.html.twig', array(
            'UserHasEvento' => $userhasevento,
            'form' => $form->createView()
        ));
}

routes:

registro:
    path: /registroEvento/{id}
    controller: AppControllerEventoController::registro    

registro2:
    path: /registroEvento/tipo-invitado/{id}
    controller: AppControllerEventoController::registro2    

Advertisement

Answer

Inside registro

return $this->redirect($this->generateUrl('registro2', ['id' => $user->getId()]));

should be changed to

return $this->redirect($this->generateUrl('registro2', ['id' => $evento->getId()]));

Or you should change

public function registro2(Request $request, Evento $evento)

to

public function registro2(Request $request, User $user)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement