Answer the question
In order to leave comments, you need to log in
symfony. How to check if the user's login is busy?
Hello, I want to understand one question using the following code as an example. I have a controller in which the user is created:
/**
* @Route("/users", name="user_create", methods={"POST"})
*/
public function createUser(Request $request)
{
$login = $request->get('login');
$password = $request->get('password');
$user = new User();
$user->setLogin($login);
$user->setPassword($password);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
$user_id = $user->getId();
return $user_id;
}
namespace App\Entity;
class User
{
/**
* @ORM\Column(type="string", length=100)
*/
private $login;
/**
* @ORM\Column(type="string", length=100)
*/
private $password;
public function setLogin(string $login): self
{
$user = MagicGetDoctrine()->getRepository(User::class)->findByLogin($login);
if ($user) {
if ($this->id != $user->getId()) {
throw new Exceptions\UserLoginIsBusyException();
}
}
$this->login = $login;
return $this;
}
}
Answer the question
In order to leave comments, you need to log in
https://symfony.com/doc/current/bundles/FOSUserBun...
https://stackoverflow.com/a/51770985/10111639
use FOS\UserBundle\Model\User as FosUser;
class User extends FosUser
{
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question