授权后我经常收到此错误:
RoleHierarchy.php第43行中的UndefinedMethodException: 尝试在类“path \ to \ User”上调用方法“getRole”。 你的意思是打电话,例如“getRoles”?
用户(没有其他字段和方法):
class User implements UserInterface {
/**
* @ORM\ManyToMany(targetEntity = "path\to\Role", inversedBy = "users")
* @ORM\JoinTable(name = "user_roles", joinColumns = {@ORM\JoinColumn(name = "userid", referencedColumName = "id")}, inverseJoinColumns = {@ORM\JoinColumn(name = "roleid", referencedColumnName = "id")})
*/
protected $roles;
public function getRoles() { return $this->roles->toArray(); }
}
角色(没有其他字段和方法):
class Role implements RoleInterface {
/**
* @ORM\Column(name = "role", type = "string", unique = true)
*/
protected $role;
/**
* @ORM\ManyToMany(targetEntity = "path\to\User", mappedBy = "roles")
*/
protected $users;
public function getRole() { return $this->role; }
}
security.yml:
security:
encoders:
path\to\User: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_ADMIN]
providers:
main_provider:
entity: { class: path\to\User, property: username }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main_signin:
pattern: ^/signin$
anonymous: ~
main:
pattern: ^/
form_login:
provider: main_provider
login_path: security_signin
check_path: security_signcheck
username_parameter: signin[username]
password_parameter: signin[password]
target_path_parameter: signin[targetpath]
default_target_path: main_index
remember_me: true
remember_me:
key: "%secret%"
lifetime: 86400
path: /
remember_me_parameter: signin[rememberme]
logout:
path: security_signout
target: security_signin
access_control:
- { path: ^/signin$ roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }
我尝试将一个方法getRole添加到用户,看看会发生什么,并得到了这个:
RoleHierarchy.php第43行中的UndefinedMethodException: 试图在类“DateTime”上调用方法“getRole”。
由于某种原因,UsernamePasswordToken-> getRoles()不返回角色数组,但返回另一个对象。我不知道我做错了什么......我试图清除开发缓存,但它没有用。
答案 0 :(得分:0)
我向用户添加了Serializable接口,错误消失了。
class User implements UserInterface, \Serializable {
...
}