Symfony2 User Roles (BETA2)

How can I add a user to a role .. for each new user, I need to do:

$em->getRepository('MyBundle:Role')->findOneBy(array('name' => 'ROLE_USER'))

everytime?

I'm not too a fan of how big UserBundle is .... and I don't use XML. I use YML / Annotations, so UserBundle is pretty hard to follow certain things.

So what is the best / cleanest way to create a user account and associate it with a default role?

+3
source share
2 answers

The easiest way I've found is to simply define it rolesas a type field arrayin a user object. Then, when you create a user (during registration or something else), it is as simple as

$roles = array('ROLE_USER');
$user->setRoles($roles);

. ( , ), .

+4

All Articles