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

时间:2014-10-14 10:54:30

标签: php symfony doctrine-orm

我正在使用symfony 2.5构建的网站,要求每个用户只需要一个角色(用户不能超过1个角色),所以在用户表中,用户名和密码都在那里名为role的另一列,其中包含管理员ROLE_ADMIN和公司员工ROLE_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();
}

我收到错误

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

如果有人可以帮我解决这个问题,我将非常感激,请注意我不需要很多关系,因为角色每个用户只有一个角色,并且希望避免创建另一个实体,如烹饪书示例。

如果有帮助,这是我的整个User实体

<?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();


    }
}

我还需要提一下,如果我不将角色转换为数组,我会得到错误

Catchable Fatal Error: Argument 4 passed to Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct() must be of the type array, string given,

2 个答案:

答案 0 :(得分:2)

您不能在非对象变量上使用toArray()方法。这是PHP。所有原始类型都没有扩展的通用对象。基元不是对象。

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

array($this->role);

但正如你的方法public function getRole()的PHPDoc中所描述的那样,你想要检索字符串。所以你不需要数组。只需返回$this->role

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

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

答案 1 :(得分:0)

当我在同一张表中使用“角色”字段时,这是Symfony 3和Symfony 4中对我的解决方案: https://stackoverflow.com/a/26359914/2400373

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

中进行这些更改。
public function getRoles()
{
   //return array('ROLE_USER');
   return array($this->role->getRol());
}

通过这种方式,数组返回登录用户的角色