Symfony login authentication returning Error: Call to a member function toArray() on a non-object

a 夏天 提交于 2020-01-29 23:37:48

问题


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');
}

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

I get the error

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

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.

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

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


回答1:


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.

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

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.

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);
}



回答2:


This was the solution for me in Symfony 3 and Symfony 4 when I use a Role field in the same table: https://stackoverflow.com/a/26359914/2400373

But in another project using Symfony 4 create an additional table called Role so that you can work with them you must make these changes in the getRoles()

public function getRoles()
{
   //return array('ROLE_USER');
   return array($this->role->getRol());
}

In this way the array returns the Role of the logged in user



来源:https://stackoverflow.com/questions/26359069/symfony-login-authentication-returning-error-call-to-a-member-function-toarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!