【问题标题】:Symfony does not remove Element from ArrayCollectionSymfony 不会从 ArrayCollection 中删除元素
【发布时间】:2015-02-11 21:22:04
【问题描述】:

我想从用户实体中的角色-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;
    }

}

【问题讨论】:

标签: symfony controller doctrine arraycollection


【解决方案1】:

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

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

附带说明,我觉得您的 getRoles() 实现是非正统的,因为建议您按原样返回 ArrayCollection 并在检索到它后调用 toArray()

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

【讨论】:

  • 感谢您的回答,我得到:Catchable Fatal Error: Argument 1 passed to Chris\UserBundle\Entity\User::removeRole() must be an instance of Chris\UserBundle\Entity\RoleInterface, instance of Chris\UserBundle\Entity\Role given, called in /Users/christianschade/bp/bp2/src/Chris/UserAdminBundle/Controller/AjaxGetController.php on line 305 and defined 当我在控制器中写入 $user->removeRole($em->getRepository('UserBundle:Role')->findRoleByName("ROLE_HAUSMEISTER" ));
  • @MikadoJoe 添加use Symfony\Component\Security\Core\Role\RoleInterface; 它会起作用的:)
  • 对不起我真的不知道
  • 在您的用户类中,在use Doctrine\Common\Collections\ArrayCollection; 行之后添加我提到的那一行。
  • 如何使用 removeRole() ?它需要一个角色接口作为参数。我只有角色对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多