Symfony2"错误:在非对象上调用成员函数toArray()"从数据库加载角色时

时间:2014-07-19 01:28:53

标签: symfony

我一直在与这个斗争几个小时。我在Symfony 2.5中构建了一个应用程序,当我以用户身份登录时收到此错误:

  

FatalErrorException:错误:在a上调用成员函数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();并将其替换为上面的行,我的用户就会毫无问题地登录系统。

现在我看到其他人在使用PHP 5.4的服务器上运行Symfony2的主要问题。我的笔记本电脑正在运行,直到我将它降级到5.3。这还没有解决问题。

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

  

可捕获的致命错误:参数4传递给   Symfony的\分量\安全\核心\认证\令牌\ UsernamePasswordToken :: __结构()   必须是一个数组,给定整数,调用   /var/www/cwwa/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php   在第96行并在中定义   /var/www/cwwa/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Token/UsernamePasswordToken.php   第36行

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

我的数据库使用两个表来保证安全。一个User表和另一个是Role表。我已经为下面的用户和角色表包含​​了实体文件:

Users.php

<?php

namespace CWWA\CoreBundle\Entity;

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

/**
 * Users
 *
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="CWWA\CoreBundle\Entity\Users")
 */
class Users implements UserInterface, \Serializable
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;


    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=500, nullable=false)
     */
    private $username;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=500, nullable=false)
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=500, nullable=false)
     */
    private $password;

    /**
     * @var string
     *
     * @ORM\Column(name="salt", type="string", length=500, nullable=false)
     */
    private $salt;

    /**
     * @var string
     *
     * @ORM\Column(name="first_name", type="string", length=125, nullable=false)
     */
    private $firstName;

    /**
     * @var string
     *
     * @ORM\Column(name="surname", type="string", length=45, nullable=false)
     */
    private $surname;

    /**
     * @var integer
     *
     * @ORM\Column(name="customer", type="integer", nullable=false)
     */
    private $customer;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime", nullable=false)
     */
    private $created;

    /**
     * @var boolean
     *
     * @ORM\Column(name="is_active", type="boolean", nullable=false)
     */
    private $isActive;

    /**
     * @var boolean
     *
     * @ORM\Column(name="blocked", type="boolean", nullable=false)
     */
    private $blocked;

    /**
     * @var integer
     *
     * @ORM\Column(name="access_list", type="integer", nullable=false)
     */
    private $accessList;

    /**
     * @ORM\ManyToMany(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 CWWA\CoreBundle\Entity;

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

/**
 * Roles
 *
 * @ORM\Table(name="roles")
 * @ORM\Entity
 */
class Roles implements RoleInterface
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

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

    /**
     * @ORM\ManyToMany(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小时后!

2 个答案:

答案 0 :(得分:0)

User实体中,roles字段应由users(实体另一边的字段)反转。

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

您可能需要更新数据库。

答案 1 :(得分:0)

我有类似的问题。 toArray()方法无效,因为$this->roles不是ArrayCollection的实例,而是整数。我不太明白为什么你的return构造函数中有三个Users语句;尝试先摆脱那些,然后设置你需要设置的属性:

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

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