我想从用户实体中的角色ArrayCollection中删除一个角色。用户和角色具有M:N连接。
在我的控制器中:
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('UserBundle:User')->find($userId);
$user->removeRole($roleID);
$em->flush();
如果我执行控制器,则不会出现错误消息。但是角色仍然分配给用户。
用户实体
namespace ChrisUserBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserUserInterface;
use DoctrineCommonCollectionsArrayCollection;
/**
* User
*
* @ORMTable(name="usermanagement_users")
* @ORMEntity(repositoryClass="ChrisUserBundleEntityUserRepository")
*/
class User implements UserInterface,Serializable
{
/**
* @ORMManyToMany(targetEntity="Role", inversedBy="users")
*
*/
private $roles;
public function __construct()
{
$this->roles = new ArrayCollection();
$this->isActive = true;
}
/**
* @inheritDoc
*/
public function getRoles()
{
return $this->roles->toArray();
}
public function setRoles($roles){
$this->roles[] = $roles;
}
public function removeRole($role){
unset($this->roles->toArray()[$role]);
}
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMColumn(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORMColumn(type="string", length=64)
*/
private $password;
/**
* @ORMColumn(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORMColumn(type="string", length=60)
*/
private $vorname;
/**
* @ORMColumn(type="string", length=60)
*/
private $nachname;
/**
* @ORMColumn(type="datetime", nullable=true)
*/
private $letzterLogin;
/**
* @ORMColumn(name="is_active", type="boolean")
*/
private $isActive;
public function setId($id){
$this->id = $id;
}
/**
* @inheritDoc
*/
public function getId()
{
return $this->id;
}
/**
* @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);
}
}
角色实体
<?php
/**
* Created by PhpStorm.
* User: christianschade
* Date: 02.02.15
* Time: 19:29
*/
namespace ChrisUserBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreRoleRoleInterface;
use DoctrineCommonCollectionsArrayCollection;
/**
* Role
* @ORMEntity(repositoryClass="ChrisUserBundleEntityRoleRepository")
* @ORMTable(name="usermanagement_roles")
*/
class Role implements RoleInterface {
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMColumn(name="role", type="string", length=20, unique=true)
*/
private $role;
public function getRole() {
return $this->role;
}
/**
* @ORMManyToMany(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;
}
/**
* @ORMManyToMany(targetEntity = "ChrisUserBundleEntityGroup", 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;
}
}
您应该使用ArrayCollection::removeElement()
来删除角色。
public function removeRole($role)
{
$this->roles->removeElement($role);
}
顺便说一句,我觉得你的getRoles()
实现是非正统的,因为建议你按原样返回ArrayCollection,并在检索后调用toArray()
(如果需要的话)。
public function getRoles()
{
return $this->roles;
}