Php Symfony登录身份验证返回错误:对非对象调用成员函数toArray()

Php Symfony登录身份验证返回错误:对非对象调用成员函数toArray(),php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我在一个使用symfony 2.5构建的网站上工作,要求每个用户只需要一个角色(用户不能有多个角色)因此,在用户名和密码所在的用户表中,有另一列名为role,其中包含管理人员的role\u ADMIN,以及公司员工的role\u STAFF,因此,在验证我的getRoles函数是否如下所示时,我可以作为管理员登录 public function getRoles() { return array('ROLE_ADMIN'); } 由于我无法硬编码角色,因此需要从数据库中获取角色,因此如

我在一个使用symfony 2.5构建的网站上工作,要求每个用户只需要一个角色(用户不能有多个角色)因此,在用户名和密码所在的用户表中,有另一列名为
role
,其中包含管理人员的
role\u ADMIN
,以及公司员工的
role\u STAFF
,因此,在验证我的
getRoles
函数是否如下所示时,我可以作为管理员登录

public function getRoles()
{
    return array('ROLE_ADMIN');
}
由于我无法硬编码角色,因此需要从数据库中获取角色,因此如果我更改此项以从用户表的角色列中获取角色

public function getRoles()
{
    return $this->getRole();
}
// getter for $role 
public function getRole()
{
    return $this->role->toArray();
}
我得到了错误

错误:对非对象调用成员函数toArray()

如果有人能帮我解决这个问题,我将不胜感激。请注意,我不需要角色的多对多关系,每个用户只需要一个角色,hance希望避免创建另一个实体,如cook book示例

如果有帮助,这就是我的整个
用户
实体

<?php

namespace ClickTeck\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;


/**
 * User
 */

class User implements AdvancedUserInterface, \Serializable
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $username;

    /**
     * @var string
     */
    private $email;

    /**
     * @var string
     */
    private $password;

    /**
     * @var string
     */
    private $salt;

    /**
     * @var boolean
     */
    private $isActive;

    /**
     * @var string
     */
    private $role;



    public function __construct()
    {
        $this->isActive = true;
        $this->salt = md5(uniqid(null, true));
        $this->role = new ArrayCollection();
    }

    public function isAccountNonExpired()
    {
        return true;
    }

    public function isAccountNonLocked()
    {
        return true;
    }

    public function isCredentialsNonExpired()
    {
        return true;
    }

    public function isEnabled()
    {
        return $this->isActive;
    }
    /**
     * @inheritDoc
     */
    public function getSalt()
    {
        return $this->salt;
    }

    public function setSalt($salt)
    {
        $this->salt = $salt;

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

        //return array('ROLE_ADMIN');
    }

    /**
     * @inheritDoc
     */
    public function eraseCredentials()
    {
    }

    /**
     * @see \Serializable::serialize()
     */
    public function serialize()
    {
        return serialize(array(
                $this->id,
                $this->username,
                $this->password,
                $this->salt,
            ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list (
            $this->id,
            $this->username,
            $this->password,
            $this->salt
            ) = unserialize($serialized);
    }
    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set username
     *
     * @param string $username
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

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

    /**
     * Set password
     *
     * @param string $password
     * @return User
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

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

    /**
     * Set email
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

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

    /**
     * Set isActive
     *
     * @param boolean $isActive
     * @return User
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;

        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean
     */
    public function getIsActive()
    {
        return $this->isActive;
    }

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

        return $this;
    }

    /**
     * Get role
     *
     * @return string
     */
    public function getRole()
    {

        return $this->role->toArray();


    }
}

您不能对非对象变量使用
toArray()
方法。这是PHP。没有可由所有基元类型扩展的通用对象。基本体不是对象

实体中描述的变量
$role
具有
string
类型。如果要使用此变量创建数组,只需调用:

array($this->role);
但正如PHPDoc for方法
公共函数getRole()
中所述,您希望检索字符串。所以不需要数组。只需返回
$this->role

要实现
UserInterface
,需要返回用户所有角色的数组。您只需在
getRoles()
方法中返回
array($this->role)

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

这是我在Symfony 3和Symfony 4中使用同一表中的角色字段时的解决方案:

但是在另一个使用Symfony 4的项目中,创建一个名为Role的附加表,以便您可以使用它们,您必须在
getRoles()


这样,数组将返回登录用户的角色

$this->Role
未初始化请向我们提供您的映射信息。如果您有字符串值,为什么您认为它将与
toArray()
方法一起工作?在许多其他语言中,您可以使用这种结构。但不是在PHP中:在PHP中,所有的基元类型都只是基元,而不是对象!他们没有任何可以调用的方法。您好,感谢您抽出时间,如果我将其作为字符串返回,我会得到错误
可捕获的致命错误:参数4传递给Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::\uu construct()必须是数组类型,字符串给定,
,几分钟前,我已经设法解决了这个问题,并将添加我的答案,你非常接近,尽管你只需要
返回数组($this->role)
在您的getRoles方法中感谢是Symfony 3.3.x上的完美解决方案
public function getRoles()
{
   //return array('ROLE_USER');
   return array($this->role->getRol());
}