Reputation: 317
I'm trying to make a method for activate users through a token, but i'm a little lost about how can i verify using doctrine 2 and relations.
Here you can see an screen of my database relations.
This is my user entity
.............
/**
* @ORM\OneToOne(targetEntity="App\Entity\Token", mappedBy="username", cascade={"persist", "remove"})
*/
private $token;
public function getToken(): ?Token
{
return $this->token;
}
public function setToken(?Token $token): self
{
$this->token = $token;
// set (or unset) the owning side of the relation if necessary
$newUsername = $token === null ? null : $this;
if ($newUsername !== $token->getUsername()) {
$token->setUsername($newUsername);
}
return $this;
}
.............
This is my token entity
.............
/**
* @ORM\OneToOne(targetEntity="App\Entity\User", inversedBy="token", cascade={"persist", "remove"})
*/
private $username;
public function getUsername(): ?User
{
return $this->username;
}
public function setUsername(?User $username): self
{
$this->username = $username;
return $this;
}
.............
The application works as expected when i register any user and the email has been sent.
In other project without realtions in the token ( saving the token in the same table as user ) i didn't have any problem making the method as shown below:
.............
/**
* @Route("/activation/{token}/", name="activation/", methods={"GET"}))
*/
public function activation(Request $request, User $user, $token)
{
$token = $request->attributes->get('token');
$update = $user->getToken();
$user->setToken(null);
$user->setActive(true);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
return $this->render(
'emails/confirmation.html.twig');
}
.............
But with this method i'm receiving the following error Unable to guess how to get a Doctrine instance from the request information for parameter.
Anyone have any idea?
Thanks for your time.
Kind regards.
..............................
EDIT1: ERROR: Unable to guess how to get a Doctrine instance from the request information for parameter "token".
CODE:
/**
* @Route("/activation/{pledge}/", name="activation/", methods={"GET"}))
*/
public function activation(Request $request, Token $token, $pledge)
{
$user = $pledge->getUsername();
if ( null === $user) {
}
$user->setToken(null);
$user->setActive(true);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
return $this->render(' emails/confirmation.html.twig');
}
}
EDIT2: SOLUTION
/**
* @Route("/activation/{token}/", name="activation/", methods={"GET"}))
*/
public function activation(Request $request, Token $pledge, $token)
{
$token = $request->attributes->get('token');
$user = $pledge->getUsername();
if ( null === $pledge) {
}
$pledge->setToken(null);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($pledge);
$entityManager->flush();
return $this->render('emails/confirmation.html.twig');
}
Upvotes: 1
Views: 321
Reputation: 611
In your old setup the User
entity could be matched by ParamConverter because, as you wrote, there was a token
field in it.
Now the token
field is a part of the Token
entity, so what you can do is match the Token
entity and get the user from it:
public function activation(Request $request, Token $token)
{
$user = $token->getUsername();
if (null === $user) {
// Here handle the situation with the token already used to activate a user
}
$user->setToken(null);
$user->setActive(true);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
return $this->render( 'emails/confirmation.html.twig');
}
....
Upvotes: 1