Doctrine orm Symfony/FosUserBundle-数据库中的存储角色

Doctrine orm Symfony/FosUserBundle-数据库中的存储角色,doctrine-orm,symfony,fosuserbundle,Doctrine Orm,Symfony,Fosuserbundle,如何在数据库中存储角色? 我试过这个方法 USER.php /** * @ORM\Entity * @ORM\Table(name="`user`") */ class User extends BaseUser { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /**

如何在数据库中存储角色? 我试过这个方法

USER.php

/**
 * @ORM\Entity
 * @ORM\Table(name="`user`")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Post", mappedBy="author")
     */
    public $article;

    /**
     * @var string
     *
     * @ORM\Column(name="full_name", type="string", length=255, nullable=true)
     */
    public $name;

    /**
     * @ORM\ManyToMany(targetEntity="Role")
     * @ORM\JoinTable(name="users_roles")
     */
    protected $roles;

    public function __construct()
    {
        parent::__construct();
        $this->roles = new ArrayCollection();
    }

    ....settings...

    /**
     * Returns an ARRAY of Role objects with the default Role object appended.
     * @return array
     */
    public function getRoles()
    {
        return array_merge($this->roles->toArray(), array(new Role(parent::ROLE_DEFAULT)));
    }

    /**
     * Returns the true ArrayCollection of Roles.
     * @return ArrayCollection
     */
    public function getRolesCollection()
    {
        return $this->roles;
    }

    /**
     * Pass a string, get the desired Role object or null.
     * @param string $role
     * @return Role|null
     */
    public function getRole($role)
    {
        foreach ($this->getRoles() as $roleItem) {
            if ($role == $roleItem->getRole()) {
                return $roleItem;
            }
        }
        return null;
    }

    /**
     * Pass a string, checks if we have that Role. Same functionality as getRole() except returns a real boolean.
     * @param string $role
     * @return boolean
     */
    public function hasRole($role)
    {
        if ($this->getRole($role)) {
            return true;
        }
        return false;
    }

    /**
     * Adds a Role OBJECT to the ArrayCollection. Can't type hint due to interface so throws Exception.
     * @throws Exception
     * @param Role $role
     */
    public function addRole($role)
    {
        if (!$role instanceof Role) {
            throw new \Exception("addRole takes a Role object as the parameter");
        }

        if (!$this->hasRole($role->getRole())) {
            $this->roles->add($role);
        }
    }

    /**
     * Pass a string, remove the Role object from collection.
     * @param string $role
     */
    public function removeRole($role)
    {
        $roleElement = $this->getRole($role);
        if ($roleElement) {
            $this->roles->removeElement($roleElement);
        }
    }

    /**
     * Pass an ARRAY of Role objects and will clear the collection and re-set it with new Roles.
     * Type hinted array due to interface.
     * @param array $roles Of Role objects.
     */
    public function setRoles(array $roles)
    {
        $this->roles->clear();
        foreach ($roles as $role) {
            $this->addRole($role);
        }
    }

    /**
     * Directly set the ArrayCollection of Roles. Type hinted as Collection which is the parent of (Array|Persistent)Collection.
     * @param Collection $role
     */
    public function setRolesCollection(Collection $collection)
    {
        $this->roles = $collection;
    }
}
ROLE.php

/**
 * Role Entity
 *
 * @ORM\Entity
 * @ORM\Table(name="roles")
 */
class Role implements RoleInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="id")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM\Column(type="string", name="role", unique=true, length=70)
     */
    private $role;

    /**
     * Populate the role field
     * @param string $role ROLE_FOO etc
     */
    public function __construct($role)
    {
        $this->role = $role;
    }

    /**
     * Return the role field.
     * @return string
     */
    public function getRole()
    {
        return $this->role;
    }

    /**
     * Return the role field.
     * @return string
     */
    public function __toString()
    {
        return (string)$this->role;
    }
}
我总是犯这样的错误

[条令\ORM\Mapping\MappingException]
已删除“***\Entity\User”中的属性“roles” 已声明,但只能声明一次

有人能帮我吗


谢谢

您需要摆脱
用户
实体中与
角色
相关的一切。现成的捆绑包支持角色

我刚刚设置了捆绑包,一切正常。这就是我的
User
实体的外观,它扩展了
BaseUser

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 */
class User extends BaseUser
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

}
绑定完成后,我运行schema update命令

如您所见,角色
是通过bundle自动添加的

您可以使用is附带的包来创建用户


可以找到捆绑包的文档。是另一个让您入门的好资源。

如果您正在阅读一篇过时的文章,请查找FosUserBundle上的最新文章,然后继续阅读。该捆绑包支持开箱即用的角色,您不应该在任何地方定义它。这就是问题所在,我找不到最新的文章,几乎所有人都链接到过时的文章。然后共享您的实体,以便人们可以查看并帮助完成,希望有人能帮助:)是的,我知道,但我想要所有角色的特殊表格。我不想用保安。因为我不想让你进入安全系统。我正在尝试创建CMS,所以我需要允许管理员创建一个新角色。然后不要fos用户绑定,创建一个自定义角色。