从数据库加载角色时 Symfony2 "Error: Call to a member function toArray() on a non-object"



我已经纠结了好几个小时了。我在Symfony 2.5中建立了一个应用程序,当我作为用户登录时,我收到这个错误:

fatalerroreexception: Error:调用成员函数toArray(非对象在/var/www/cwwa/src/CWWA/CoreBundle/Entity/Users.php行402

在402行,我有这样的代码:

/**
 * @inheritDoc
 */
public function getRoles()
{
    //return array('ROLE_CUSTOMER_USER');
    return $this->roles->toArray();
}

如果我注释掉return $this->roles->toArray();并用它上面的行替换它,我的用户可以毫无问题地登录到系统。

现在我看到其他人遇到的主要问题是Symfony2在PHP 5.4的服务器上运行。我的笔记本电脑一直在运行这个,直到我把它降级到5.3。这并没有解决问题。

我使用的另一种方法是将return $this->roles->toArray();替换为return $this->roles;,我得到这个错误:

Catchable Fatal Error:参数4传递给Symfony核心组件 安全 认证牌 UsernamePasswordToken:: __construct ()必须是一个数组,整数给定,调用/var/www/cwwa/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/供应商/UserAuthenticationProvider.php在第96行中定义/var/www/cwwa/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/令牌/UsernamePasswordToken.php第36行

我假设系统正在从数据库中检索一个值。

My Database使用两个表来保证安全性。一个是User表,另一个是Role表。我已经为下面的User和Role表包含了Entity文件:

Users.php

<?php
namespace CWWACoreBundleEntity;
use SymfonyComponentSecurityCoreUserUserInterface;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
 * Users
 *
 * @ORMTable(name="users")
 * @ORMEntity(repositoryClass="CWWACoreBundleEntityUsers")
 */
class Users implements UserInterface, Serializable
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORMColumn(name="username", type="string", length=500, nullable=false)
     */
    private $username;
    /**
     * @var string
     *
     * @ORMColumn(name="email", type="string", length=500, nullable=false)
     */
    private $email;
    /**
     * @var string
     *
     * @ORMColumn(name="password", type="string", length=500, nullable=false)
     */
    private $password;
    /**
     * @var string
     *
     * @ORMColumn(name="salt", type="string", length=500, nullable=false)
     */
    private $salt;
    /**
     * @var string
     *
     * @ORMColumn(name="first_name", type="string", length=125, nullable=false)
     */
    private $firstName;
    /**
     * @var string
     *
     * @ORMColumn(name="surname", type="string", length=45, nullable=false)
     */
    private $surname;
    /**
     * @var integer
     *
     * @ORMColumn(name="customer", type="integer", nullable=false)
     */
    private $customer;
    /**
     * @var DateTime
     *
     * @ORMColumn(name="created", type="datetime", nullable=false)
     */
    private $created;
    /**
     * @var boolean
     *
     * @ORMColumn(name="is_active", type="boolean", nullable=false)
     */
    private $isActive;
    /**
     * @var boolean
     *
     * @ORMColumn(name="blocked", type="boolean", nullable=false)
     */
    private $blocked;
    /**
     * @var integer
     *
     * @ORMColumn(name="access_list", type="integer", nullable=false)
     */
    private $accessList;
    /**
     * @ORMManyToMany(targetEntity="Roles", inversedBy="roles")
     *
     */
    private $roles;
    public function __construct()
    {
        return $this->isActive = true;
        return $this->salt = md5(uniqid(null, true));
        return $this->roles = new ArrayCollection();
    }
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set username
     *
     * @param string $username
     * @return Users
     */
    public function setUsername($username)
    {
        $this->username = $username;
        return $this;
    }
    /**
     * Get username
     *
     * @return string 
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return Users
     */
    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }
    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }
    /**
     * Set password
     *
     * @param string $password
     * @return Users
     */
    public function setPassword($password)
    {
        $this->password = $password;
        return $this;
    }
    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }
    /**
     * Set salt
     *
     * @param string $salt
     * @return Users
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;
        return $this;
    }
    /**
     * Get salt
     *
     * @return string 
     */
    public function getSalt()
    {
        return $this->salt;
    }
    /**
     * Set firstName
     *
     * @param string $firstName
     * @return Users
     */
    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;
        return $this;
    }
    /**
     * Get firstName
     *
     * @return string 
     */
    public function getFirstName()
    {
        return $this->firstName;
    }
    /**
     * Set surname
     *
     * @param string $surname
     * @return Users
     */
    public function setSurname($surname)
    {
        $this->surname = $surname;
        return $this;
    }
    /**
     * Get surname
     *
     * @return string 
     */
    public function getSurname()
    {
        return $this->surname;
    }
    /**
     * Set customer
     *
     * @param integer $customer
     * @return Users
     */
    public function setCustomer($customer)
    {
        $this->customer = $customer;
        return $this;
    }
    /**
     * Get customer
     *
     * @return integer 
     */
    public function getCustomer()
    {
        return $this->customer;
    }
    /**
     * Set created
     *
     * @param DateTime $created
     * @return Users
     */
    public function setCreated($created)
    {
        $this->created = $created;
        return $this;
    }
    /**
     * Get created
     *
     * @return DateTime 
     */
    public function getCreated()
    {
        return $this->created;
    }
    /**
     * Set isActive
     *
     * @param boolean $isActive
     * @return Users
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;
        return $this;
    }
    /**
     * Get isActive
     *
     * @return boolean 
     */
    public function getIsActive()
    {
        return $this->isActive;
    }
    /**
     * Set blocked
     *
     * @param boolean $blocked
     * @return Users
     */
    public function setBlocked($blocked)
    {
        $this->blocked = $blocked;
        return $this;
    }
    /**
     * Get blocked
     *
     * @return boolean 
     */
    public function getBlocked()
    {
        return $this->blocked;
    }
    /**
     * Set accessList
     *
     * @param integer $accessList
     * @return Users
     */
    public function setAccessList($accessList)
    {
        $this->accessList = $accessList;
        return $this;
    }
    /**
     * Get accessList
     *
     * @return integer 
     */
    public function getAccessList()
    {
        return $this->accessList;
    }
    /**
     * Set roles
     *
     * @param integer $roles
     * @return Users
     */
    public function setRoles($roles)
    {
        $this->roles = $roles;
        return $this;
    }

    /**
     * @inheritDoc
     */
    public function getRoles()
    {
        //return array('ROLE_CUSTOMER_USER');
        return $this->roles;
    }
    /**
     * @inheritDoc
     */
    public function eraseCredentials()
    {
    }
    /**
     * @see Serializable::serialize()
     */
    public function serialize()
    {
        return serialize(array(
            $this->id,
        ));
    }
    /**
     * @see Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list (
            $this->id,
        ) = unserialize($serialized);
    }
    public function isEqualTo(UserInterface $user)
    {
        return $this->id === $user->getId();
    }
}

Roles.php

<?php
namespace CWWACoreBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use SymfonyComponentSecurityCoreRoleRoleInterface;
/**
 * Roles
 *
 * @ORMTable(name="roles")
 * @ORMEntity
 */
class Roles implements RoleInterface
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;
    /**
     * @var string
     *
     * @ORMColumn(name="names", type="string", length=30, nullable=false)
     */
    private $name;
    /**
     * @var string
     *
     * @ORMColumn(name="role", type="string", length=20, nullable=false)
     */
    private $role;
    /**
     * @ORMManyToMany(targetEntity="Users", mappedBy="roles")
     */
    private $users;
    public function __construct()
    {
        $this->users = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set names
     *
     * @param string $names
     * @return Roles
     */
    public function setNames($names)
    {
        $this->names = $names;
        return $this;
    }
    /**
     * Get names
     *
     * @return string 
     */
    public function getNames()
    {
        return $this->names;
    }
    /**
     * Set role
     *
     * @param string $role
     * @return Roles
     */
    public function setRole($role)
    {
        $this->role = $role;
        return $this;
    }
    /**
     * Get role
     *
     * @return string 
     */
    public function getRole()
    {
        return $this->role;
    }
}

任何帮助都太棒了,尤其是在撞了4个小时的砖墙之后!

在您的User实体中,roles字段应该被users(实体另一侧的字段)反转。

/**
 * @ORMManyToMany(targetEntity="Roles", inversedBy="users")
 */
private $roles;

你可能需要更新你的数据库

我也遇到过类似的问题。toArray()方法不起作用,因为$this->roles不是ArrayCollection的实例,而是一个整数。我不太明白为什么在你的Users构造函数中有三个return语句;试着先去掉这些,只设置你需要设置的属性:

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

现在toArray()方法应该工作,因为$this->roles应该是ArrayCollection()。请记住,Symfony安全层需要的是角色对象数组或字符串数组。

相关内容

最新更新