UserEntityの作成
UserInterface,\Serializableを継承
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity("email")
*/
class User implements UserInterface,\Serializable
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $password;
/**
* @ORM\Column(type="integer")
*/
private $userRoll;
/**
* @ORM\Column(type="string", length=255,unique=true)
*/
// ユニークキーの設定
private $email;
public function serialize()
{
return serialize([
$this->id,
$this->email,
$this->password,
$this->userRoll,
]);
}
public function unserialize($serialized)
{
list($this->id,
$this->email,
$this->password,
$this->userRoll
) = unserialize($serialized,['allowed_classes'=>false]);
}
public function eraseCredentials()
{}
public function getSalt()
{
return null;
}
public function getRoles()
{
return [$this->getUserRoll()];
}
public function getUsername()
{}
}
コントローラー
ユーザー作成
/**
* ユーザー登録
*
* @param Request $req
* @param UserPasswordEncoderInterface $passwordEncoder
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function create(Request $req,UserPasswordEncoderInterface $passwordEncoder)
{
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($req);
if ($form->isSubmitted() && $form->isValid()) {
$password = $passwordEncoder->encodePassword($user, $user->getPassword());
$user->setPassword($password);
$manager = $this->getDoctrine()->getManager();
$manager->persist($user);
$manager->flush();
return $this->redirectToRoute(
'admin_user_find' , [
'id' => $user->getId(),
]);
}
return $this->render($this->twig_base . 'create.html.twig', [
'form' => $form->createView()
]);
}