php - Symfony 5 get fail object not found by the @ParamConverter annotation
Get the solution ↓↓↓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
App\Entity\Evento 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: App\Controller\EventoController::registro
registro2:
path: /registroEvento/tipo-invitado/{id}
controller: App\Controller\EventoController::registro2
Answer
Solution:
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)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot add or update a child row: a foreign key constraint fails
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.