Symfony登录身份验证返回错误:在非对象上调用成员函数toArray() [英] Symfony login authentication returning Error: Call to a member function toArray() on a non-object

查看:49
本文介绍了Symfony登录身份验证返回错误:在非对象上调用成员函数toArray()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用symfony 2.5构建的网站上,要求每个用户只需要拥有一个角色(用户不能拥有多个角色),因此在users表中,用户名和密码位于另一列名为 role 的列,其中列有 ROLE_ADMIN 用于管理员,而 ROLE_STAFF 用于公司员工,因此在验证我的 getRoles 函数是否如下所示时,我能够以管理员身份正常登录

I am working on a website that is built using symfony 2.5, the requirement was that each user needs to have a only one role (user cannot have more than 1 role) so in users table where the username and passwords go there is another column by the name role which holds ROLE_ADMIN for admin people and ROLE_STAFF for company staff, so while authenticating if my getRoles function looks like the following i am able to login just fine as an admin

public function getRoles()
{
    return array('ROLE_ADMIN');
}

因为我无法对角色进行硬编码,因此需要从数据库中获取它,因此如果我更改此设置以从 user 表的角色列中获得角色

since i cannot hard code the role and it needs to be fetched from database so if i change this to get the role from the role column of user table

public function getRoles()
{
    return $this->getRole();
}
// getter for $role 
public function getRole()
{
    return $this->role->toArray();
}

我收到错误


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

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

如果有人可以帮助我解决此问题,我将不胜感激,请注意,我不需要多对多关系船,因为每个用户只需要一个角色,因此希望避免创建另一个实体,例如Cook book示例。

I will really appreciate if someone can help me fix this, please note that i do not need many to many relation ships for the role just one role per user and hance want to avoid creating another entity like cook book example.

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

If it helps this is my entire User Entity

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


    }
}

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

I also need to mention suppose if i dont turn the roles to array i get the error


可捕获的致命错误:参数4传递给Symfony\Component\ \安全性\核心\身份验证\令牌\UsernamePasswordToken :: __ construct()必须为数组类型,给定字符串,

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


推荐答案

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

You cannot use toArray() method on your non-object variable. It's PHP. There are no universal object that extended by all primitive types. Primitives are not objects.

您的实体中描述的变量 $ role 具有字符串类型。如果要使用此变量创建数组,则可以调用:

Your variable $role as described in your Entity has string type. If you want to create array with this variable you can just call:

array($this->role);

但是如您的PHPDoc中所述,方法 public function getRole()您要检索字符串。因此,您不需要数组。只需返回 $ this->角色

But as described in your PHPDoc for method public function getRole() you want to retrieve string. And so you doesn't need array. Just return $this->role.

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

To implement UserInterface you need to return array of all the roles for your user. You can just return array($this->role) in your getRoles() method:

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

这篇关于Symfony登录身份验证返回错误:在非对象上调用成员函数toArray()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆