0
votes

I feel the same as a forum user who posted this:

I implemented FOSUserBundle, and I want to add to RegisrationFormType roles that are taken from a table. When I had it like this:

->add('roles', 'choice', array('label' => 'Rol', 'required' => true, 
                           'choices' => array( 'ROLE_ADMIN' => 'ADMINISTRADOR','ROLE_SUPERADMIN' => 'SUPERADMINISTRADOR', 
                                               'ROLE_USER' => 'USUARIO'), 'multiple' => true))

And it works! But they must leave the BD, I can not put the Entity field because roles should be an array, not an object. How I can generate the array with the roles taken from a table? In FosUSerbundle as you would add roles?

Thanks ....

I write because that user had no answer. I followed [the steps of official documentation] (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md) and adding the above lines in the register of FOSUserBundle works, but I want to work this from the database.

And then I used to create groups this. Two additional tables were created and even now joined a group or role in the list, but not how to show the login to register a new user.

Has anyone solved it?

1

1 Answers

0
votes

So you have the roles in a table? You could inject the EntityManager in the form type and use it to fetch the choices.

        ->add('roles', 'choice', array(
            'label' => 'Rol',
            'choices' => $this->getRoles(),
            'multiple' => true,
            'required' => true,
        ))

Then create a method which gives you the data the way you need.

Something like:

public function getRoles()
{
    $roles = array();
    $er = $this->em->getRepository('AppBundle:Role');
    $results = $er->createQueryBuilder('r')
            // conditions here
            ->getQuery()
            ->getResult();

    // process the array?
    foreach ($results as $role) {
        $roles[$role->getId()] = $role->getLabel();
    }

    return $roles;
}