我与用户<->角色<->组现在,我的数据库中有以下关系表(除了用户、组和角色):
role_group,group_ role,角色用户,用户角色
这是对的吗?我了解到,对于M:N,我只需要两个关系表。
我之所以这么做,是因为当我试图加载固定装置时,我遇到了一个Doctrine''ORM''NonUniqueResultException错误。
我认为这是ORM ManyToMany符号中的一个错误。
以下是ORM:用户实体
namespace Chris'UserBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use Symfony'Component'Security'Core'User'UserInterface;
use Doctrine'Common'Collections'ArrayCollection;
use Symfony'Component'Security'Core'Role'RoleInterface;
/**
* User
*
* @ORM'Table(name="usermanagement_users")
* @ORM'Entity(repositoryClass="Chris'UserBundle'Entity'UserRepository")
*/
class User implements UserInterface,'Serializable
{
/**
* @ORM'ManyToMany(targetEntity="Role", inversedBy="users")
*
*/
private $roles;
public function getRoles()
{
return $this->roles->toArray();
}
public function __construct()
{
$this->roles = new ArrayCollection();
$this->isActive = true;
}
/**
* @inheritDoc
*/
public function getRolesOld()
{
return $this->roles->toArray();
}
public function setRoles($roles){
$this->roles[] = $roles;
}
public function removeRole(RoleInterface $role)
{
$this->roles->removeElement($role);
}
/**
* @ORM'Column(type="integer")
* @ORM'Id
* @ORM'GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM'Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM'Column(type="string", length=64)
*/
private $password;
/**
* @ORM'Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM'Column(type="string", length=60)
*/
private $vorname;
/**
* @ORM'Column(type="string", length=60)
*/
private $nachname;
/**
* @return mixed
*/
public function getVorname()
{
return $this->vorname;
}
/**
* @param mixed $vorname
*/
public function setVorname($vorname)
{
$this->vorname = $vorname;
}
/**
* @return mixed
*/
public function getNachname()
{
return $this->nachname;
}
/**
* @param mixed $nachname
*/
public function setNachname($nachname)
{
$this->nachname = $nachname;
}
/**
* @return mixed
*/
public function getLetzterLogin()
{
return $this->letzterLogin;
}
/**
* @param mixed $letzterLogin
*/
public function setLetzterLogin($letzterLogin)
{
$this->letzterLogin = $letzterLogin;
}
/**
* @return mixed
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* @param mixed $isActive
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
}
/**
* @ORM'Column(type="datetime", nullable=true)
*/
private $letzterLogin;
/**
* @ORM'Column(name="is_active", type="boolean")
*/
private $isActive;
public function setId($id){
$this->id = $id;
}
/**
* @inheritDoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritDoc
*/
public function getUsername()
{
return $this->username;
}
public function setUsername($username)
{
$this->username = $username;
}
/**
* @inheritDoc
*/
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
/**
* @inheritDoc
*/
public function getPassword()
{
return $this->password;
}
public function setPassword($password)
{
$this->password = $password;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
}
/**
* @inheritDoc
*/
public function eraseCredentials()
{
}
/**
* @see 'Serializable::serialize()
*/
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/**
* @see 'Serializable::unserialize()
*/
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
}
角色实体
namespace Chris'UserBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use Symfony'Component'Security'Core'Role'RoleInterface;
use Doctrine'Common'Collections'ArrayCollection;
/**
* Role
* @ORM'Entity(repositoryClass="Chris'UserBundle'Entity'RoleRepository")
* @ORM'Table(name="usermanagement_roles")
*/
class Role implements RoleInterface {
/**
* @ORM'Column(type="integer")
* @ORM'Id
* @ORM'GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM'Column(name="role", type="string", length=20, unique=true)
*/
private $role;
public function getRole() {
return $this->role;
}
/**
* @ORM'ManyToMany(targetEntity = "User", inversedBy = "roles")
*
* @var ArrayCollection $users
*/
protected $users;
/**
* @return ArrayCollection
*/
public function getUsers()
{
return $this->users;
}
/**
* @param ArrayCollection $users
*/
public function setUsers($users)
{
$this->users = $users;
}
public function setRoles($roles)
{
$this->role = $roles;
// allows for chaining
return $this;
}
/**
* @inheritDoc
*/
public function getId()
{
return $this->id;
}
/**
* @ORM'ManyToMany(targetEntity = "Chris'UserBundle'Entity'Group", inversedBy = "groups")
*
* @var ArrayCollection $group
*/
private $group;
/**
* @return ArrayCollection
*/
public function getGroup()
{
return $this->group;
}
/**
* @param ArrayCollection $group
*/
public function setGroup($group)
{
$this->group = $group;
}
}
集团实体
namespace Chris'UserBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use Doctrine'Common'Collections'ArrayCollection;
/**
* Group
* @ORM'Table(name="usermanagement_groups")
* @ORM'Entity
*/
class Group {
public function __construct()
{
$this->role = new ArrayCollection();
}
/**
* @ORM'Column(type="integer")
* @ORM'Id
* @ORM'GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM'ManyToMany(targetEntity="Role", mappedBy="users")
*
*/
private $role;
/**
* @ORM'Column(type="string", length=40, unique=true)
*/
private $groupName;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return ArrayCollection
*/
public function getRole()
{
return $this->role;
}
/**
* @param ArrayCollection $role
*/
public function setRole($role)
{
$this->role[] = $role;
}
public function getGroupName()
{
return $this->groupName;
}
public function setGroupName($group)
{
$this->groupName = $group;
}
}
您需要在一个符号上使用"mappedBy",在另一个符号中使用"invertedBy"。
/**
* @ORM'ManyToMany(targetEntity="Role", inversedBy="users")
*
*/
private $roles;
/**
* @ORM'ManyToMany(targetEntity = "User", mappedBy = "roles")
*
* @var ArrayCollection $users
*/
protected $users;
除此之外,我认为您在属性"role"下的"Group"中的注释中有一个错误(它映射到用户)。
Groupt实体中缺少角色的映射。