Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoleInterface throws "call on a non-object" error

Tags:

symfony

I'm working over Symfony 2.0.16

I have in my UserProvider the method getRoles

public function getRoles()
{
    /**
     * @var \Doctrine\Common\Collections\ArrayCollection $rol
     */
    return $this->rol->toArray();
}

and my Rol entity has the role interface

class Rol implements \Symfony\Component\Security\Core\Role\RoleInterface
//...
public function getRole()
{
    return $this->getName();
}

but when I try to login I get the following error

Fatal error: Call to a member function getRole() on a non-object in C:\Users\julian\Code\parqueadero\vendor\symfony\src\Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector.php on line 57

Reading the class SecurityDataCollector, the error is thrown by a Closure

array_map(function ($role){ return $role->getRole();}, $token->getRoles()

Now I change this to

array_map(function ($role){ var_dump($role); return $role->getRole();}, $token->getRoles()

To my surprise, $role is a object Rol but I can't understand why I get the error.

like image 319
rkmax Avatar asked Sep 03 '12 17:09

rkmax


1 Answers

I found the solution the problem is a bug in PHP 5.4 (the php i'm using) serialize method the github user yoannch proposed this solution, is overwrite the serialize/unserialize methods using json_encode/json_decode methods

class User implements \Serializable

//...

/**
 * Serializes the content of the current User object
 * @return string
 */
public function serialize()
{
    return \json_encode(
            array($this->username, $this->password, $this->salt,
                    $this->rol, $this->id));
}

/**
 * Unserializes the given string in the current User object
 * @param serialized
 */
public function unserialize($serialized)
{
    list($this->username, $this->password, $this->salt,
                    $this->rol, $this->id) = \json_decode(
            $serialized);
}

only need change the correct name properties

like image 52
rkmax Avatar answered Oct 20 '22 10:10

rkmax