Мне удалось создать пользователей с ролями в таблице ролей, но теперь я хотел бы связаться с пользователями, но я не вижу, как получить роль. Вот мой код.
Мне кажется, проблема в том, что я неправильно прочитал имя роли в таблице ролей, но не знаю, как это сделать.
Итак, вот концептуальные данные модель:
class Role
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="array", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $description;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
*
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?array
{
return $this->name;
}
public function setName(array $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
/*
* methods for RoleInterface
*/
public function getRole() : array
{
$this->getName();
}
Entity User
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $username;
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\Column(type="boolean", nullable=false, options={"default" : 0})
*/
private $isActive;
/**
* @ORM\Column(type="string", unique=true, length=64)
*/
private $token;
/**
* @ORM\Column(type="datetime")
*/
private $expiresAt;
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users", fetch="LAZY")
* @ORM\JoinTable(name="user_role")
*/
private $roles = [];
public function __construct()
{
$this->roles = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string)$this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* @inheritDoc
*/
public function getRoles(): ?Role
{
if (empty($this->roles)){
return ['ROLE_USER'];
}
return $this->roles;
}
/**
* @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 getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getToken(): ?string
{
return $this->token;
}
public function setToken(string $token): self
{
$this->token = $token;
return $this;
}
public function getExpiresAt(): ?\DateTimeInterface
{
return $this->expiresAt;
}
public function setExpiresAt(\DateTimeInterface $expiresAt): self
{
$this->expiresAt = $expiresAt;
return $this;
}
public function isExpired(): bool
{
return $this->getExpiresAt() <= new \DateTime();
}
public function createToken()
{
return substr(str_replace(['+', '/'], ['-', '_'], base64_encode(random_bytes(50))), 0, 63);
}
public function setRoles($roles)
{
if (is_array($roles)) {
$this->roles = $roles;
} else {
$this->roles->clear();
$this->roles->add($roles);
}
return $this;
}
/**
* Add userRoles
*
* @param \App\Entity\Role $roles
* @return User
*/
public function addRoles(\App\Entity\Role $roles)
{
$this->roles[] = $roles;
return $this;
}
/**
* Remove userRoles
*
* @param \App\Entity\Role $roles
*/
public function removeRoles(App\Entity\Role $roles)
{
$this->roles->removeElement($roles);
}