Symfony不会从ArrayCollection中删除Element

时间:2015-02-11 21:22:04

标签: symfony controller doctrine arraycollection

我想从用户实体中的roles-ArrayCollection中删除角色。 用户和角色具有M:N连接。

在我的控制器中:

  $em = $this->getDoctrine()->getManager();
  $user = $em->getRepository('UserBundle:User')->find($userId);
  $user->removeRole($roleID);
  $em->flush();

如果我执行控制器,则没有错误消息。 但角色仍然分配给用户。

用户实体     

namespace Chris\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
 * 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 __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]);

    }

    /**
     * @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;


    /**
     * @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 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 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;
    }

}

1 个答案:

答案 0 :(得分:2)

您应该使用ArrayCollection::removeElement()删除该角色。

public function removeRole($role)
{
    $this->roles->removeElement($role);
}

另外,我觉得你的getRoles()实现是非正统的,因为建议按原样返回ArrayCollection,如果你需要检索它,请调用toArray()

public function getRoles()
{
    return $this->roles;
}