Assign role to user in doctrine

From start of days of symfony sfauth plugin play a special role in making developer’s life easy. It give structured way to assign roles and permissions to user.
So at the time of creation of user add/edit forms you can see the groups (roles) and permissions to choose. Also permissions can also direct work for whole group.
But if we need to set permissons/roles to user without using forms then how it can be implemented?
Well yesterday i am searching any workaround for this.
Normally we can use code like that
$sf_user = new sfGuardUser();
But i need to set groups against user. Here are a problem, group have many to many relationship with user. Si how i can set the group using similiar code. First i tried following combinations but no one seems to work. Some gave errors some not.
Here $group is not a normal variable but a doctrine collection of group.
$sf_user->Groups = $group;

As user can have multiple groups so setting group also reflect the same way.
$sf_user->Groups[] = $group;
Here we can see we are not assigning but adding a group against a user. So above example works.