I constantly get this error after authorization:
UndefinedMethodException in RoleHierarchy.php line 43: Attempted to call method "getRole" on class "path\to\User". Did you mean to call e.g. "getRoles"?
User (without other fields and methods):
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(); }
}
Role (without other fields and methods):
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 }
I tried to add a method getRole to the User to see what happens, and got this:
UndefinedMethodException in RoleHierarchy.php line 43: Attempted to call method "getRole" on class "DateTime".
For some reason UsernamePasswordToken->getRoles() does not return an array of roles but another object is returned. I don't know what I'm doing wrong... I tried to clear a dev cache but it didn't help.