Reputation: 149
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 !
Upvotes: 2
Views: 24878
Reputation: 1041
I have the route to fetch user profile
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. I added User object parameter default value as null and included a null check in the controller method.
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.
Upvotes: 0
Reputation: 11
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")
Upvotes: 1
Reputation: 1291
The old {id} part of the route has to change to the same name as the parameter you are converting.
I.e. 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+"})
Upvotes: 0
Reputation: 274
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"})
*/
Upvotes: 2