Home » PHP » Assign role to user in doctrine

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();
$sf_user->setUsername($username);
$sf_user->save();
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->setSfUserGroup($group);
$sf_user->setGroup($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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s