4

I would like to create a form to edit my users. Users and roles connected with ManyToMany. In UserUsers entity I have a $roles variable which is ArrayCollection:

public function __construct()
{
    $this->roles = new ArrayCollection();
}

On my form I would like to add roles to my users via multiple select form element. In my user form:

public function buildForm( FormBuilderInterface $builder, array $options ) {
    $builder->add( 'username' )
            ->add( 'password', 'repeated', array( 
                    'type' => 'password',
                    'mapped' => false,
                    'required' => false,
                    'first_options' => array( 
                            'label' => 'Password' ),
                    'second_options' => array( 
                            'label' => 'Repeat Password' ) ) )
            ->add( 'roles', 'choice', array( 
                    'mapped' => false,
                    'multiple' => true ) );
}

Now my multiple select is empty.

If I turn mapped to true, I got an error message:

UserRoles could not be converted to int in...

I've tried a lots of ways, but I could not solve this problem correctly.

11
  • What type do you have in your $roles ArrayCollection? Commented May 9, 2013 at 13:11
  • All elements in $roles ArrayCollection is an entity (UserRoles). Commented May 9, 2013 at 14:26
  • For a choice of entities you should use the special choice field type 'entity' (symfony.com/doc/current/reference/forms/types/entity.html). For an example see my answer to a similar question - stackoverflow.com/questions/13519961/… Commented May 9, 2013 at 15:50
  • Okay, I changed roles like this: $builder->add( 'roles', 'entity', array( 'class' => 'AcmeUserBundle:UserRoles', 'multiple' => true, 'property' => 'name' ) ); Then I got an error message: Expected argument of type "Doctrine\Common\Collections\Collection", "array" given Commented May 10, 2013 at 7:35
  • That's odd, as ArrayCollection implements Doctrine\Common\Collections\Collection. Is it possible that roles() is reassigned to an array elsewhere? Or that ArrayCollection is not resolving to the doctrine class? Commented May 10, 2013 at 8:14

2 Answers 2

6

For a choice of entities you should use the special choice field type 'entity' (see Symfony manual for entity field type). For an example see my answer to a similar question. If you get further errors you may also find this question on Role Interface and Manage Roles helpful.

Sign up to request clarification or add additional context in comments.

Comments

1

for the fosuserbundle I do it like that:

        $builder->add('roles', 'choice', array(
        'multiple' => true,
        'choices' => array(
            'ROLE_USER' => 'User',
            'ROLE_AUTHOR' => 'Autor',
            'ROLE_MODERATOR' => 'Moderator',
            'ROLE_ADMIN' => 'Admin'
        ),
        'label' => 'Rollen',
        'required' => true
    ));

2 Comments

This would be good, but I store the roles in the database and when I open the users to edit, I want to be selected those roles which are assigned to the current user.
I have already understood and I strongly recommend the use of FOSUserBundle - Because of the security. There you can store multiple roles to a user. This is the end what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.