gpt4 book ai didi

symfony - RoleInterface 抛出 "call on a non-object"错误

转载 作者:行者123 更新时间:2023-12-02 10:01:35 25 4
gpt4 key购买 nike

我正在使用 Symfony 2.0.16

我的 UserProvider 中有 getRoles 方法

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

我的 Rol 实体具有角色接口(interface)

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

但是当我尝试登录时出现以下错误

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

读取SecurityDataCollector类,错误是由Closure抛出的

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

现在我将其更改为

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

令我惊讶的是,$role 是一个对象 Rol,但我不明白为什么会收到错误。

最佳答案

我发现问题的解决方案是 PHP 5.4 中的错误(我正在使用的 PHP)序列化方法 github 用户 yoannch提出这个solution ,是使用 json_encode/json_decode 方法覆盖 serialize/unserialize 方法

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

只需要更改正确的名称属性

关于symfony - RoleInterface 抛出 "call on a non-object"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12251825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com