这是实体文件:
<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
#[Assert\Email(message:"the email '{{ value }}' is required")]
private ?string $email = null;
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(length: 255)]
public ?string $full_name = null;
#[ORM\Column(type: Types::DATE_MUTABLE)]
public ?\DateTimeInterface $date_naiss = null;
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 getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
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;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getFullName(): ?string
{
return $this->full_name;
}
public function setFullName(string $full_name): self
{
$this->full_name = $full_name;
return $this;
}
public function getDateNaiss(): ?\DateTimeInterface
{
return $this->date_naiss;
}
public function setDateNaiss(\DateTimeInterface $date_naiss): self
{
$this->date_naiss = $date_naiss;
return $this;
}
}
这是树枝文件(我删除了模板代码,因为它太长了,无法发布):
{% extends 'back.html.twig' %}
{% block body %}
<tr>
<th>Id</th>
<th>Full Name</th>
<th>Email</th>
<th>Date of Birth</th>
<th></th>
<th>Action<th>
</tr>
{% for i in user %}
<tr>
<td>{{i.id}}</td>
<td>{{i.full_name}}</td>
<td>{{i.email}}</td>
<td>{{i.date_naiss | date('Y-m-d') }}</td>
<td>
{% for role in user.roles %}
{% if role == "ROLE_USER" %}
Utilisateur
{% elseif role == "ROLE_EDITOR" %}
Editeur
{% elseif role == "ROLE_ADMIN" %}
Administrateur
{% endif %}
{% endfor %}
</td>
<td><a href="{{ path('user_delete', {'id': i.Id}) }}">Delete</a>
<a href="{{ path('user_update', {'id': i.Id}) }}">Update</a></td>
</tr>
{% endfor %}
{% endblock %}
这是我在控制器中使用的列表函数:
#[Route('admin/list', name: 'user_list')]
public function userlist(UserRepository $user): Response
{
return $this->render('admin/list.html.twig', [
'user' => $user->findall()
]);
}
我在 symfony 5 中使用预定义的实体 User 和角色数组,每当我尝试运行路由“/admin/list”时,它都会显示上面的错误,即使角色数组是在实体文件中定义的。