如何防止FOSUserbundle在Symfony3.3

时间:2017-06-08 17:25:35

标签: php symfony doctrine-orm orm fosuserbundle

我是编程新手并决定使用Symfony框架启动PHP。

我的目标是建立一个用户系统,用户和他们的角色之间存在多对多的关系。理想情况下,我喜欢将角色存储在自己的表格中。

我设法设置了这些,但确实有问题。 每当FOSUserBundle创建用户并添加角色时,我的代码现在将在Roles表中创建重复条目。例如:

我创建了一个用户:User1,并分配角色" ROLE_ADMIN" - A" ROLE_ADMIN"记录在角色表中创建。

我创建了一个用户:User2并分配了相同的角色 - 而不是使用现有的ROLE_ADMIN记录,创建了一条新记录。

我想在创建新角色之前让实体管理器通过角色表,但是,正如我在堆栈溢出中读取其他响应一样,这是非常不鼓励的。其他答案也不鼓励在实体中使用服务

这是我的两个实体的代码(我是编程原谅我的错误)

user.php的

<?php
namespace ShagCore\MemberBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\Common\Collections\ArrayCollection as ArrayCollection;

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

    /**
     * @ORM\ManyToMany(targetEntity="Role", inversedBy="users", cascade={"persist"})
     * @ORM\JoinTable(name="member_user_role")
     */
    protected $userRoles;

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

    public function addRole($roleStr)
    {
        //get the role as a string, loop through the $this->roles (Role Objects)
        //ensure that this role name isn't there, THEN add it as a new Role Obj 
        //to the $this->roles array

        $roleStr = strtoupper($roleStr);   

        $currRoles = [];

        foreach($this->userRoles as $userRole) {
            $currRoles[] = $userRole->getRole();
        }

        if(!in_array($roleStr, $currRoles)) {
            $role = new Role();
            $role->setRole($roleStr);
            $this->userRoles[] = $role;
        }
    }

    public function getRoles()
    {
        $roles = array();
        foreach($this->userRoles as $role) {
            $roles[] = $role->getRole();
        }

        return array_unique($roles);
    }
}

Role.php

<?php
namespace ShagCore\MemberBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection as ArrayCollection;


//TODO: removeRole()

/**
 * @ORM\Entity
 * @ORM\Table(name="member_role")
 */
class Role
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", unique=TRUE, length=100)
     */
    protected $role;
    /**
     * @ORM\ManyToMany(targetEntity="User", mappedBy="userRoles", cascade={"persist"})
     */
    protected $users;

    /**
     * @ORM\Column(type="text", nullable=TRUE)
     */
    protected $description;

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


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

    /**
     * Set role
     *
     * @param string $role
     *
     * @return Role
     */
    public function setRole($role)
    {
        $this->role = $role;

        return $this;
    }

    /**
     * Get role
     *
     * @return string
     */
    public function getRole()
    {
        return $this->role;
    }

    /**
     * Set description
     *
     * @param string $description
     *
     * @return Role
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Add user
     *
     * @param \ShagCore\MemberBundle\Entity\User $user
     *
     * @return Role
     */
    public function addUser(\ShagCore\MemberBundle\Entity\User $user)
    {
        $this->users[] = $user;

        return $this;
    }

    /**
     * Remove user
     *
     * @param \ShagCore\MemberBundle\Entity\User $user
     */
    public function removeUser(\ShagCore\MemberBundle\Entity\User $user)
    {
        $this->users->removeElement($user);
    }

    /**
     * Get users
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getUsers()
    {
        return $this->users;
    }
}

0 个答案:

没有答案