Symfony2 \ Doctrine - 检索用户角色

时间:2013-11-29 13:51:34

标签: php symfony doctrine

我正在尝试设置我的用户实体以使用角色,并按照http://symfony.com/doc/current/cookbook/security/entity_provider.html

上的文档进行操作

用户工作正常,如果我对$ roles值进行硬编码,一切都按预期工作,登录/退出是好的,等等。但是,如果我尝试通过多对多关系检索角色,如文档我回来了。

我还应该提一下,在我运行'php app / console doctrine:schema:update --force'后创建实体后,它创建了角色表,但没有创建它所说的'user_role'表。我继续手动创建并为我正在测试的用户输入了一行,但这是我的第一个线索,有些东西无法正常工作。这真是令人沮丧,因为我遵循了文档,它看起来应该可以工作。

我在尝试登录时遇到的错误是:

FatalErrorException: Error: Call to a member function toArray() on a non-object

指向用户实体中的return $this->roles->toArray()

我的用户实体(相关位):

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="ACME\MyBundle\Entity\UserRepository")
 *
*/

class User implements UserInterface, \Serializable
{

 ...
      /**
     * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
     *
     */
    private $roles;
...
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->roles = new ArrayCollection();
    }

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

我的角色实体:

use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

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

    /**
     * @ORM\Column(name="name", type="string", length=30)
     */
    private $name;

    /**
     * @ORM\Column(name="role", type="string", length=20, unique=true)
     */
    private $role;

    /**
     * @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
     */
    private $users;

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

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

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

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

        return $this;
    }

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

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

        return $this;
    }
}

是否有人在我的代码中发现问题或遇到同样的问题?我现在卡住了。

2 个答案:

答案 0 :(得分:1)

在您的实体角色中

 * @ORM\Table(name="role")

将其更改为

* @ORM\Table(name="user_role")

因为您的表名是user_role而不是角色

答案 1 :(得分:0)

我有同样的问题,我删除了toArray方法

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="ACME\MyBundle\Entity\UserRepository")
 *
*/

class User implements UserInterface, \Serializable
{

 ...
      /**
     * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
     *
     */
    private $roles;
...
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->roles = new ArrayCollection();
    }

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