user419017
user419017

Reputation:

$roles must be an array of strings, or RoleInterface instances, but got object

This is with Symfony2 and Doctrine2. I have this in my user entity:

public function getRoles()
{
    return $this->roles->toArray();
}

Yet however, it is returning:

 array(object(Role))

Any ideas where I can start looking? I've been debugging for a while.

Upvotes: 1

Views: 3371

Answers (2)

rdromao
rdromao

Reputation: 608

I bumped into this error myself after following the Cookbook Tutorial on Loading Users From Database, and maybe this is your case.

It turns out I forgot to implement the RoleInterface on my Role entity object (the one referenced by my User entity). That was it.

Upvotes: 1

Jérémy Dutheil
Jérémy Dutheil

Reputation: 6137

This is the logical behavior ; by default, Doctrine2 returns a Collection object, and by calling toArray() you transform it into an array. But your array still contains Role objects, that are basically not strings. Of course, you can define your own methods to get it as a string, I think basically you just have to call Role::getRole().

Have a look at this article, it may help you.

Upvotes: 2

Related Questions