了解 Symfony 5 Doctrine ManyToOne 关系概念与用户和角色
Understanding Symfony 5 Doctrine ManyToOne relation concepts with Users and Roles
我试图理解 Doctrine ORM 关系概念,因为据我所知,我花了很多时间来创建一个非常简单的用例,但是,我没能做到,因为我有知识匮乏...
这是我的问题:
我有 2 个实体,UserApp 和 RoleApp。
我希望有可能管理来自用户的角色,而不是硬编码角色,以数组格式直接进入用户 table。所以要做到这一点,我有一个经典的外键 role_id
到 user_app
table,它将主键引用到 role_app
table.
我需要为每个用户获取他的角色,并以 Symfony 身份验证所需的正确格式引入它:
class UserApp implements UserInterface, \Serializable{
.....
/**
* @var UserRole
* @ORM\ManyToOne(targetEntity="App\Entity\UserRole")
* @ORM\JoinColumn(nullable=false)
*
*/
private $role;
public function getUserRole(): array{ // I know it's wrong but I don't know what is the right type to get
return $this->role->getUserRole($this);
}
/**
* Returns the roles or permissions granted to the user for security.
*/
public function getRoles(): array{
//$role = ["ADMIN_USER"];
$role = $this->getUserRole();
dump($role);
return array_unique($role);
class UserRole{
/**
* @ORM\Column(type="json")
*/
private $appRole = [];
public function getUserRole(UserApp $user): ?array
{
$userRoleRepository = new UserRoleRepository();
$userRole[] = $userRoleRepository->find(1);
dump($userRole);
return $userRole;
}
}
但是这段代码没有为 Symfony 身份验证的用户角色需求提供正确的格式:(
终于如愿以偿了;这是我的解决方案,我希望它能帮助...
class UserApp implements UserInterface, \Serializable
{
....
/**
* @var UserRole
* @ORM\ManyToOne(targetEntity="App\Entity\UserRole")
* @ORM\JoinColumn(nullable=false)
*
*/
private $role;
public function getUserRole(): UserRole
{
return $this->role->getRoleUser($this);
}
/**
* Returns the roles or permissions granted to the user for security.
* it's for authentification compliance
*/
public function getRoles(): array
{
$arrRole = $this->getUserRole()->getAppRole();
return array_unique($arrRole);
}
......
class UserRole{
.....
/**
* @ORM\Column(type="json")
*/
private $appRole = [];//For authentification compliance
.....
public function getRoleUser(UserApp $user): ?self
{
return $this;
}`enter code here`
我试图理解 Doctrine ORM 关系概念,因为据我所知,我花了很多时间来创建一个非常简单的用例,但是,我没能做到,因为我有知识匮乏...
这是我的问题: 我有 2 个实体,UserApp 和 RoleApp。
我希望有可能管理来自用户的角色,而不是硬编码角色,以数组格式直接进入用户 table。所以要做到这一点,我有一个经典的外键 role_id
到 user_app
table,它将主键引用到 role_app
table.
我需要为每个用户获取他的角色,并以 Symfony 身份验证所需的正确格式引入它:
class UserApp implements UserInterface, \Serializable{
.....
/**
* @var UserRole
* @ORM\ManyToOne(targetEntity="App\Entity\UserRole")
* @ORM\JoinColumn(nullable=false)
*
*/
private $role;
public function getUserRole(): array{ // I know it's wrong but I don't know what is the right type to get
return $this->role->getUserRole($this);
}
/**
* Returns the roles or permissions granted to the user for security.
*/
public function getRoles(): array{
//$role = ["ADMIN_USER"];
$role = $this->getUserRole();
dump($role);
return array_unique($role);
class UserRole{
/**
* @ORM\Column(type="json")
*/
private $appRole = [];
public function getUserRole(UserApp $user): ?array
{
$userRoleRepository = new UserRoleRepository();
$userRole[] = $userRoleRepository->find(1);
dump($userRole);
return $userRole;
}
}
但是这段代码没有为 Symfony 身份验证的用户角色需求提供正确的格式:(
终于如愿以偿了;这是我的解决方案,我希望它能帮助...
class UserApp implements UserInterface, \Serializable
{
....
/**
* @var UserRole
* @ORM\ManyToOne(targetEntity="App\Entity\UserRole")
* @ORM\JoinColumn(nullable=false)
*
*/
private $role;
public function getUserRole(): UserRole
{
return $this->role->getRoleUser($this);
}
/**
* Returns the roles or permissions granted to the user for security.
* it's for authentification compliance
*/
public function getRoles(): array
{
$arrRole = $this->getUserRole()->getAppRole();
return array_unique($arrRole);
}
......
class UserRole{
.....
/**
* @ORM\Column(type="json")
*/
private $appRole = [];//For authentification compliance
.....
public function getRoleUser(UserApp $user): ?self
{
return $this;
}`enter code here`