php - Symfony2 Assigning User Roles During Registration -


I have followed Symfony2 and done entities, databases and forms while doing my job The problem is that in my database I did not have any users, so I completed these steps, but in my opinion it should be better connected. Registration docs did not say anything about roles I think that the best time to play an ideal role will be during user registration. This is what I am doing, but I have lost some degree of how to do that there is no way to add roles to the user unit consideration. My question is how do I specify a default role ("ROLE_USER") for users during registration, which is stored in the database?

The relevant code:

User unit:

Registration and login actions:

SignupCreature is where I suspect that having a magic Should or perhaps the user code of the __ generation () method I just got lost in order to do this correctly.

Two things first you need to create an accessory approach for both classes. This means:

  / ** * ... * / class TblUser {// ... public function getRoles () {return $ this-> Roles & gt; ToArray (); } Public Function Setrol (Roles of Collection $) {foreach ($ roles as role $) {$ this- & gt; AddRole ($ role); }} Public function addRole (TblPrivilege $ role) {if (! $ $-> $-> roles) in this ($!); {$ This- & gt; Roles-gt; Plus ($ role); $ Role- & gt; Adduser ($ this); }} Public function removeRole (TblPrivilege $ role) {if ($ role-> $ role-) in $ (- $ role)) {$ this-> Roles- & gt; Remove Element ($ role); $ Role- & gt; removeUser ($ this); }}} / ** * ... * / class TblPrivileges {// ... public function getUsers () {return $ this- & gt; the user; } Public function set user (collection $ users) {foreach ($ user $ as user) {$ this- & gt; addUser ($ user); }} Public Function addUser (TblUser $ user) {If (! $ $ This-> User- & gt; contains ($ user)) {$ this- & gt; User- & gt; Plus ($ user); }} Public Function Deletion User (TblUser $ user) {if ($ user- & gt; in ($ user) is) {$ this- & gt; User- & gt; Delete Element ($ user); In fact $ user- & gt; In order to call the addRole () method, I actually go to a theory program client, which will actually carry the actual role (and thus) during the registration) during registration. This is well on the document.  

To register an event subscriber, you should see:

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -