我尝试通过make:command将管理员角色添加到现有用户。问题:[错误]没有使用该电子邮件地址的用户。
我做了什么。
使用easyAdminBundle创建用户。没关系
我的实体:
namespace App\Entity;
use Cocur\Slugify\Slugify;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity("email")
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Assert\Email(message="Veuillez rentrer une adresse email valide")
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\Column(type="string", length=255)
*/
private $lastName;
/**
* @ORM\Column(type="string", length=255)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
/**
* @ORM\Column(type="string", length=255)
*/
private $avatarImage;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Article", mappedBy="author", orphanRemoval=true)
*/
private $articles;
public function __construct()
{
$this->articles = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = "ROLE_USER";
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function addRoles(string $roles): self
{
if (!in_array($roles, $this->roles)) {
$this->roles[] = $roles;
}
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getAvatarImage(): ?string
{
return $this->avatarImage;
}
public function setAvatarImage(string $avatarImage): self
{
$this->avatarImage = $avatarImage;
return $this;
}
/**
*@ORM\PrePersist
*@ORM\PreUpdate
*/
public function initializeSlug(){
if(empty($this->slug)){
$slugify=new Slugify();
$this->slug=$slugify->slugify($this->lastName .' '.$this->firstName);
}
}
/**
* @return Collection|Article[]
*/
public function getArticles(): Collection
{
return $this->articles;
}
public function addArticle(Article $article): self
{
if (!$this->articles->contains($article)) {
$this->articles[] = $article;
$article->setAuthor($this);
}
return $this;
}
public function removeArticle(Article $article): self
{
if ($this->articles->contains($article)) {
$this->articles->removeElement($article);
// set the owning side to null (unless already changed)
if ($article->getAuthor() === $this) {
$article->setAuthor(null);
}
}
return $this;
}
}
创建命令:php bin /控制台make:command
// src/Command/UserPromoteCommand.php
namespace App\Command;
use App\Entity\User;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class UserPromoteCommand extends Command
{
protected static $defaultName = 'app:user:promote';
private $manager;
public function __construct(EntityManagerInterface $manager)
{
$this->manager = $manager;
parent::__construct();
}
protected function configure()
{
$this
->setDescription('Promote a user by adding him a new roles.')
->addArgument('email', InputArgument::REQUIRED, 'Email address of the user you want to promote.')
->addArgument('roles', InputArgument::REQUIRED, 'The roles you want to add to the user.')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$email = $input->getArgument('email');
$roles = $input->getArgument('roles');
$userRepository = $this->manager->getRepository(User::class);
$user = $userRepository->find($email);
if ($user) {
$user->addRoles($roles);
$this->manager->flush();
$io->success('The roles has been successfully added to the user.');
} else {
$io->error('There is no user with that email address.');
}
return 0;
}
}
php bin /控制台应用程序的结果:用户:promote [email protected] ROLE_ADMIN:[错误]没有使用该电子邮件地址的用户。
是什么问题? EntityManagerInterface也许?我不是。
我不太确定,但我认为您需要执行$this->manager->persist($user);
在$this->manager->flush();
之前>
并且对于查找用户$user = $userRepository->find($email);
,我认为您需要使用
$user = $userRepository->findOneBy(['email'=>$email]);
祝你有美好的一天。